Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jfm
Posting Yak Master
145 Posts |
Posted - 2013-04-22 : 08:03:30
|
Hi there, I have 15 different tables. I need to create a new_table selecting the columns that have the same name from each of the 15 existing tables. I cannot remember the query for this case. Any tip?Thanks! |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-22 : 08:09:25
|
you can use(SELECT.....INTO statement) as follows:SELECT columnsListINTO new_tableFROM tab1 t1 JOIN ........ --Chandu |
|
|
Grifter
Constraint Violating Yak Guru
274 Posts |
Posted - 2013-04-22 : 08:10:22
|
think you're looking for select into:http://www.w3schools.com/sql/sql_select_into.asp |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2013-04-22 : 08:27:38
|
I dont remember how to type the query for 15 tables:SELECT columnsListINTO new_tableFROM tab1e_1 ? table_2 ? Table15?JOIN ??Thank youquote: Originally posted by bandi you can use(SELECT.....INTO statement) as follows:SELECT columnsListINTO new_tableFROM tab1 t1 JOIN ........ --Chandu
|
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
jfm
Posting Yak Master
145 Posts |
Posted - 2013-04-22 : 08:59:38
|
Chandu, msg 4145, level 15, state 1, line 29 Select Red,black,white,greeninto new_tablefrom table1join table2 on table1.Red.black.white.green=table3.Red.black.white.greenjoin table4 on table3.Red.black.white.green=table4.Red.black.white.greenjoin table5 on table4.Red.black.white.greenThank you |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-22 : 09:10:30
|
[code]table1.Red.black.white.green = table3.Red.black.white.green this should be your tableName.CommonColumnName-- see this illustrationCREATE TABLE [Department]( [DepartmentID] [int] NOT NULL PRIMARY KEY, [Name] VARCHAR(250) NOT NULL, ) ON [PRIMARY] INSERT [Department] ([DepartmentID], [Name]) VALUES (1, N'Engineering') INSERT [Department] ([DepartmentID], [Name]) VALUES (2, N'Administration') INSERT [Department] ([DepartmentID], [Name]) VALUES (3, N'Sales') INSERT [Department] ([DepartmentID], [Name]) VALUES (4, N'Marketing') INSERT [Department] ([DepartmentID], [Name]) VALUES (5, N'Finance') GO CREATE TABLE [Employee]( [EmployeeID] [int] NOT NULL PRIMARY KEY, [FirstName] VARCHAR(250) NOT NULL, [LastName] VARCHAR(250) NOT NULL, [DepartmentID] [int] NOT NULL REFERENCES [Department](DepartmentID), ) ON [PRIMARY] GO INSERT [Employee] ([EmployeeID], [FirstName], [LastName], [DepartmentID]) VALUES (1, N'Orlando', N'Gee', 1 ) INSERT [Employee] ([EmployeeID], [FirstName], [LastName], [DepartmentID]) VALUES (2, N'Keith', N'Harris', 2 ) INSERT [Employee] ([EmployeeID], [FirstName], [LastName], [DepartmentID]) VALUES (3, N'Donna', N'Carreras', 3 ) INSERT [Employee] ([EmployeeID], [FirstName], [LastName], [DepartmentID]) VALUES (4, N'Janet', N'Gates', 3 )--DEPARTMENT_ID is commmon column Between Employee and Department tablesSELECT * FROM Department D JOIN Employee E ON D.DepartmentID = E.DepartmentID [/code]if you are not able do, provide us first 3 tables structure? We will show you some example syntax...EDIT: you can refer this link http://www.sqlservercentral.com/Forums/Topic936633-149-1.aspx--Chandu |
|
|
|
|
|
|
|