SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Columns and Tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

145 Posts

Posted - 04/22/2013 :  08:03:30  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 04/22/2013 :  08:09:25  Show Profile  Reply with Quote
you can use(SELECT.....INTO statement) as follows:

SELECT columnsList
INTO new_table
FROM tab1 t1
JOIN ........


--
Chandu
Go to Top of Page

Grifter
Constraint Violating Yak Guru

251 Posts

Posted - 04/22/2013 :  08:10:22  Show Profile  Reply with Quote
think you're looking for select into:

http://www.w3schools.com/sql/sql_select_into.asp
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 04/22/2013 :  08:27:38  Show Profile  Reply with Quote
I dont remember how to type the query for 15 tables:

SELECT columnsList
INTO new_table
FROM tab1e_1 ? table_2 ? Table15?
JOIN ??

Thank you



quote:
Originally posted by bandi

you can use(SELECT.....INTO statement) as follows:

SELECT columnsList
INTO new_table
FROM tab1 t1
JOIN ........


--
Chandu

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 04/22/2013 :  08:33:25  Show Profile  Reply with Quote
FROM table1 t1
JOIN table t2 ON t1.CommonCol = t2.CommonCol
JOIN table3 t3 ON t2.CommonCol1 = t3.CommCol
.
.
.
Refer http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

--
Chandu
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 04/22/2013 :  08:59:38  Show Profile  Reply with Quote
Chandu,

msg 4145, level 15, state 1, line 29

Select Red,black,white,green
into new_table
from table1
join table2 on table1.Red.black.white.green
=
table3.Red.black.white.green
join table4 on table3.Red.black.white.green
=
table4.Red.black.white.green
join table5 on table4.Red.black.white.green

Thank you
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 04/22/2013 :  09:10:30  Show Profile  Reply with Quote
table1.Red.black.white.green = table3.Red.black.white.green
 this should be your tableName.CommonColumnName

-- see this illustration
CREATE 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 tables
SELECT * 
FROM Department D 
JOIN Employee E ON D.DepartmentID = E.DepartmentID 

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

Edited by - bandi on 04/22/2013 09:14:05
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000