Skip to content

Database

Discussions on database access, SQL, and ADO

This category can be followed from the open social web via the handle database@forum.codeproject.com

17.1k Topics 61.8k Posts
  • Sql connections connectors / 2 networks

    database sysadmin question javascript help
    1
    0 Votes
    1 Posts
    0 Views
    No one has replied
  • 0 Votes
    6 Posts
    0 Views
    CHill60C
    Use the database information to generate the SQL for you. For example (using the Northwind sample database from MS): USE [Northwind] DECLARE @listStr VARCHAR(MAX) ;WITH Source AS ( -- Generate list of all the columns from the table -- each preceded by our chosen table alias SELECT CASE WHEN TABLE_NAME = 'Orders' THEN 'O.' WHEN TABLE_NAME = 'Customers' THEN 'C.' WHEN TABLE_NAME = 'Employees' THEN 'E.' WHEN TABLE_NAME = 'Order Details' THEN 'OD.' ELSE '' END + COLUMN_NAME AS ColName FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN ('Orders', 'Customers','Employees','Order Details') AND TABLE_SCHEMA='dbo' ) -- generate a comma-separated list of those columns SELECT @listStr = COALESCE(@listStr+',' ,'') + ColName FROM Source -- build the rest of the SQL statement DECLARE @SQL varchar(max) SET @SQL = 'SELECT ' + @listStr + ' FROM Orders O ' SET @SQL = @SQL + 'JOIN Customers C on O.CustomerID=C.CustomerID ' SET @SQL = @SQL + 'JOIN Employees E on O.EmployeeID=E.EmployeeID ' SET @SQL = @SQL + 'JOIN [Order Details] OD on OD.OrderID = O.OrderID ' -- use PRINT rather than select as it is easier to copy PRINT @SQL Produces this output (line breaks inserted for clarity): SELECT C.CustomerID,C.CompanyName,C.ContactName,C.ContactTitle,C.Address,C.City,C.Region, C.PostalCode,C.Country,C.Phone,C.Fax, E.EmployeeID,E.LastName,E.FirstName,E.Title,E.TitleOfCourtesy,E.BirthDate, E.HireDate,E.Address,E.City,E.Region, E.PostalCode,E.Country,E.HomePhone,E.Extension,E.Photo,E.Notes,E.ReportsTo,E.PhotoPath, OD.OrderID,OD.ProductID,OD.UnitPrice,OD.Quantity,OD.Discount, O.OrderID,O.CustomerID,O.EmployeeID,O.OrderDate,O.RequiredDate,O.ShippedDate, O.ShipVia,O.Freight,O.ShipName, O.ShipAddress,O.ShipCity,O.ShipRegion,O.ShipPostalCode,O.ShipCountry FROM Orders O JOIN Customers C on O.CustomerID=C.CustomerID JOIN Employees E on O.EmployeeID=E.EmployeeID JOIN [Order Details] OD on OD.OrderID = O.OrderID There are other ways of getting the column names (you should really use the object id instead of the name for example), and you can use FOR XML PATH to generate the CSV - but for a quick and dirty one-off this works
  • Can I syn Oracle DB to SQL server

    database sql-server oracle sysadmin
    8
    0 Votes
    8 Posts
    0 Views
    CHill60C
    Hope it works :thumbsup:
  • Throw error if date fails the conversion or invalid date string

    help database
    4
    0 Votes
    4 Posts
    1 Views
    Richard DeemingR
    The simplest option is to change your parameter's type to date. Then, if the caller tries to pass in a value that isn't a date, an error will be thrown, and the procedure will not be executed. You should always use an appropriate data type for the data you're dealing with. Storing dates in strings is a sign of a bad design. "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
  • 0 Votes
    1 Posts
    0 Views
    No one has replied
  • Get PrimaryKey column name by using its IndexName

    help question
    1
    0 Votes
    1 Posts
    0 Views
    No one has replied
  • Get modified column names from Change Data Capture

    help announcement
    3
    0 Votes
    3 Posts
    1 Views
    I
    Oh sorry man, its Change Data Capture :), somehow I am able to reach it with SQL coding but if I can capture the column names only with the some settings it will be easier. Thanks in advance buddies Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
  • 0 Votes
    5 Posts
    0 Views
    CHill60C
    Is this a response? Why quote Scott Mitchell without the link to the article - Maintaining a Log of Database Changes - Part 1 - 4GuysFromRolla.com[^]
  • 0 Votes
    6 Posts
    0 Views
    M
    Ok guys - I guess the verdict's in. It's what I thought initially thought would be the case too. Its just that the doco on Correlated vs Uncorrelated sub-queries threw me a little, where correlated sub-queries are re-evaluated for each row of the main query. I do agree, however, that re-evaluating the sub-query will be significantly better than searching the full result set. Cheers people.
  • Unable to write data on to the Excel

    database help sharepoint com sysadmin
    6
    0 Votes
    6 Posts
    0 Views
    I
    But one small limitation here, bcp is creating files only in xls format (97-2003) but any body know how to do the same thing to get document in xlsx format like (2007) format? For now I can live with it but just want to acquire more knowledge on it. Any help is appreciated, thanks in advance friends :) Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
  • Migrate columns which have carriage return and line feed

    algorithms help tutorial
    4
    0 Votes
    4 Posts
    0 Views
    I
    Sorry, yeah it was code issue, thanks for all your support friends. Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
  • 0 Votes
    9 Posts
    0 Views
    J
    Oracle has two major temporal types, Date and Timestamp, where Timestamp has higher precision but less functionality. The easiest way to get a date without the time component is Trunc(MyDate) Wrong is evil and must be defeated. - Jeff Ello
  • 0 Votes
    2 Posts
    0 Views
    L
    Much the same, see ASP.NET Database Connection[^].
  • 0 Votes
    11 Posts
    0 Views
    H
    Thanks you. The other is not Service Name, SID. Everything is ok now.
  • Convert Text to datetime

    question mysql com announcement
    4
    0 Votes
    4 Posts
    0 Views
    N
    It does have a CONVERT method but it won't work with that format of text dates. That was the first thing I tried. -NP Never underestimate the creativity of the end-user.
  • 0 Votes
    12 Posts
    17 Views
    G
    Sure :) 300,000 rows is probably going to stretch things a bit. “That which can be asserted without evidence, can be dismissed without evidence.” ― Christopher Hitchens
  • Analysis services in SQL Express

    database sql-server sysadmin tools
    2
    0 Votes
    2 Posts
    0 Views
    J
    Analysis services and OLAP is not supported in the Express versions[^] of SQL-Server. Wrong is evil and must be defeated. - Jeff Ello
  • Questions about: Information System concept.

    sales help tutorial question
    3
    0 Votes
    3 Posts
    0 Views
    J
    Thank you for your answer Richard MacCutchan. I have my own notes about Information System concept (I wrote it from there), I invented a case study related to the concept, to know if I understand well the concept or not.
  • Order by by using levels using sql Script

    database tools help question
    4
    0 Votes
    4 Posts
    0 Views
    CHill60C
    OP asked for Quote: I want to Order by Name for all managers, again under each manager I want to order all his Employees order by name again? Your query is only listing the workers
  • Recursive Common Table Expressions

    tutorial question
    11
    0 Votes
    11 Posts
    0 Views
    A
    Greetings, Thanks a lot and as you said indeed no need to use the recursive common table expression within the code I shared and I already changed it and I used the CROSS APPLY operator and it works 100% fine and in 0 time instead of 24 seconds :) it was my fault what I wrote firstly :). However, I've been curious on knowing how the recursive common table expression works behind the scene :) as I said I know how it works in its simplest form but I wanted to know what if the anchor member will return more than one result that will be joined with the recursive member and so on... how things will be done. Thanks for help now I knew what I need :). Best regards, Amr Mohammad Rashad