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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Columns and Tables

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 columnsList
INTO new_table
FROM tab1 t1
JOIN ........


--
Chandu
Go to Top of Page

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
Go to Top of Page

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 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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-22 : 08:33:25
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 - 2013-04-22 : 08:59:38
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
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 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
[/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
Go to Top of Page
   

- Advertisement -