Click here to Skip to main content
15,896,207 members
Home / Discussions / Database
   

Database

 
AnswerRe: Get the user name and date time who modified or inserted data Pin
Peter_in_278021-Mar-16 15:49
professionalPeter_in_278021-Mar-16 15:49 
AnswerRe: Get the user name and date time who modified or inserted data Pin
John C Rayan24-Mar-16 6:23
professionalJohn C Rayan24-Mar-16 6:23 
QuestionCreating Table with inner join of all Child tables giving error Pin
indian14321-Mar-16 13:24
indian14321-Mar-16 13:24 
AnswerRe: Creating Table with inner join of all Child tables giving error Pin
CHill6021-Mar-16 13:34
mveCHill6021-Mar-16 13:34 
GeneralRe: Creating Table with inner join of all Child tables giving error Pin
indian14321-Mar-16 14:21
indian14321-Mar-16 14:21 
GeneralRe: Creating Table with inner join of all Child tables giving error Pin
Sascha Lefèvre21-Mar-16 14:51
professionalSascha Lefèvre21-Mar-16 14:51 
GeneralRe: Creating Table with inner join of all Child tables giving error Pin
indian14321-Mar-16 15:35
indian14321-Mar-16 15:35 
AnswerRe: Creating Table with inner join of all Child tables giving error Pin
CHill6022-Mar-16 0:49
mveCHill6022-Mar-16 0:49 
Use the database information to generate the SQL for you.

For example (using the Northwind sample database from MS):
SQL
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):
SQL
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
QuestionCan I syn Oracle DB to SQL server Pin
hmanhha20-Mar-16 6:35
hmanhha20-Mar-16 6:35 
AnswerRe: Can I syn Oracle DB to SQL server Pin
CHill6020-Mar-16 11:34
mveCHill6020-Mar-16 11:34 
GeneralRe: Can I syn Oracle DB to SQL server Pin
hmanhha20-Mar-16 18:40
hmanhha20-Mar-16 18:40 
GeneralRe: Can I syn Oracle DB to SQL server Pin
CHill6020-Mar-16 22:06
mveCHill6020-Mar-16 22:06 
GeneralRe: Can I syn Oracle DB to SQL server Pin
hmanhha21-Mar-16 5:12
hmanhha21-Mar-16 5:12 
GeneralRe: Can I syn Oracle DB to SQL server Pin
CHill6021-Mar-16 5:29
mveCHill6021-Mar-16 5:29 
GeneralRe: Can I syn Oracle DB to SQL server Pin
hmanhha21-Mar-16 12:51
hmanhha21-Mar-16 12:51 
GeneralRe: Can I syn Oracle DB to SQL server Pin
CHill6021-Mar-16 13:18
mveCHill6021-Mar-16 13:18 
QuestionChange Data Capture to handle parent and child tables Pin
indian14319-Mar-16 19:04
indian14319-Mar-16 19:04 
QuestionThrow error if date fails the conversion or invalid date string Pin
indian14318-Mar-16 15:20
indian14318-Mar-16 15:20 
AnswerRe: Throw error if date fails the conversion or invalid date string Pin
Mycroft Holmes18-Mar-16 18:03
professionalMycroft Holmes18-Mar-16 18:03 
GeneralRe: Throw error if date fails the conversion or invalid date string Pin
indian14319-Mar-16 19:05
indian14319-Mar-16 19:05 
AnswerRe: Throw error if date fails the conversion or invalid date string Pin
Richard Deeming21-Mar-16 3:25
mveRichard Deeming21-Mar-16 3:25 
QuestionGet modified column names from Change Data Capture Pin
indian14317-Mar-16 8:42
indian14317-Mar-16 8:42 
AnswerRe: Get modified column names from CDC Pin
Mycroft Holmes17-Mar-16 12:43
professionalMycroft Holmes17-Mar-16 12:43 
GeneralRe: Get modified column names from Change Data Capture Pin
indian14317-Mar-16 12:59
indian14317-Mar-16 12:59 
QuestionHow to design database to store userlog of changing data? Pin
hmanhha15-Mar-16 9:38
hmanhha15-Mar-16 9:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.