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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Creating One Table from Many Tables

Author  Topic 

AlexP
Starting Member

34 Posts

Posted - 2005-09-09 : 12:10:25
Database Designing Question:

Is there a script out there that can combine TableA, TableB and TableC into one table called TableCombined? I need it to create only the columns that are unique to all 3 tables.

Thanks...

X002548
Not Just a Number

15586 Posts

Posted - 2005-09-09 : 12:24:49
Post the DDL and what your final table is going to look like.

Read the hint link below.



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-09 : 12:42:13
"columns that are unique to all 3 tables"

Is that

a) Column names that exist in at least one of the three tables, but do not exist in any other table in the DB

b) column names that exist in [are common to] all three tables

c) column names that exist in only one of the three tables

d) something else

Kristen
Go to Top of Page

AlexP
Starting Member

34 Posts

Posted - 2005-09-09 : 13:32:16
Kristen,

Some column names are shared among the 3 tables.
I simply want to combine all the columns from 3 tables into one table, without duplicating the common columns.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-09 : 13:42:45
You'll need to manually remove the last comma as I didn't write the code to remove it since it would complicate it too much for something that is easy to correct by hand.



SET NOCOUNT ON

CREATE TABLE t1 (Column1 int)
CREATE TABLE t2 (Column1 int, Column2 int)
CREATE TABLE t3 (Column2 int, Column3 int)

SELECT 'CREATE TABLE t1Combined ('
UNION ALL
SELECT DISTINCT COLUMN_NAME + ' ' + DATA_TYPE + ' ' + CASE WHEN IS_NULLABLE = 'YES' THEN ' NULL, ' ELSE ' NOT NULL, ' END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('t1', 't2', 't3')
UNION ALL
SELECT ')'

DROP TABLE t1, t2, t3



Tara
Go to Top of Page

AlexP
Starting Member

34 Posts

Posted - 2005-09-09 : 13:59:16
Forgive my stupidity but why does your code not create the t1Combined when I run it in QA?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-09 : 14:09:50
Tara's script will just generate the script - which you then have to run.

I've got a variation, this will also generate a script (AND I'm too lazy to remove the last comma!). It will copy all the data from the three tables into a new table. The new table will need Primary Key creating etc. whereas Tara's script will generate the structure for the table - not sure which you need, but it might give you some ideas

USE Northwind
GO

SELECT 'SELECT'
UNION ALL
SELECT CHAR(9) + '[' + MIN(TABLE_NAME) + '].[' + COLUMN_NAME + '],'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('Customers', 'Orders', 'Order Details')
GROUP BY COLUMN_NAME
UNION ALL
SELECT 'INTO MyNewTable
FROM Customers
LEFT OUTER JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
LEFT OUTER JOIN [Order Details]
ON [Order Details].OrderID = Orders.OrderID'

it generates the script

SELECT
[Customers].[Address],
[Customers].[City],
[Customers].[CompanyName],
[Customers].[ContactName],
[Customers].[ContactTitle],
[Customers].[Country],
[Customers].[CustomerID],
[Order Details].[Discount],
[Orders].[EmployeeID],
[Customers].[Fax],
[Orders].[Freight],
[Orders].[OrderDate],
[Order Details].[OrderID],
[Customers].[Phone],
[Customers].[PostalCode],
[Order Details].[ProductID],
[Order Details].[Quantity],
[Customers].[Region],
[Orders].[RequiredDate],
[Orders].[ShipAddress],
[Orders].[ShipCity],
[Orders].[ShipCountry],
[Orders].[ShipName],
[Orders].[ShippedDate],
[Orders].[ShipPostalCode],
[Orders].[ShipRegion],
[Orders].[ShipVia],
[Order Details].[UnitPrice], -- Delete this final comma
INTO MyNewTable
FROM Customers
LEFT OUTER JOIN Orders
ON Orders.CustomerID = Customers.CustomerID
LEFT OUTER JOIN [Order Details]
ON [Order Details].OrderID = Orders.OrderID

Kristen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-09 : 14:11:47
Because that would entail a bunch more code. Is this a one time thing only? If not, then you could put the results of the select into a variable, then EXEC (@varName) to execute it dynamically.

Tara
Go to Top of Page

AlexP
Starting Member

34 Posts

Posted - 2005-09-09 : 14:29:40
thanks everyone for the education. Very good code and boy did it save a ton of my time.
Go to Top of Page
   

- Advertisement -