Solved

How do i move a field and its contents to a different table?

Posted on 2011-02-13
5
237 Views
Last Modified: 2012-05-11
I have two tables e.g. Product and ProductNew both are linked via a foreign key call ProductId

However i want to merge the tables into one table?

What the sql for this please?

THanks
Webbo
0
Comment
Question by:Webbo_1980
  • 2
  • 2
5 Comments
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 100 total points
ID: 34882391
SQL Server offers the INTO clause, you can create a new table from a select via SELECT (...) INTO new_table.

I'm not sure but it seems you want a inner join on the productid. This assumes you have different columns in Product and ProductNew, only a common ProductID, and want to join them to a single table with all the columns.


SELECT *
FROM Product t1 Full Outer Join ProductNew t2 On t1.ProductID =t2.ProductID

If that's what you want you can create a new table from it by

SELECT * INTO ProductLatest
FROM Product t1 Full Outer Join ProductNew t2 On t1.ProductID =t2.ProductID


If both tables have the same structure, then you simply do a UNION of the tables, in fact you wouldn't need a new table at all, but insert ProductNew data into the already existing Product table.

Bye, Olaf.


 
0
 

Author Comment

by:Webbo_1980
ID: 34882431
Thanks Olaf but so just to make sure i understnad this completly...

I have a table called Product and ProductEx

I want to merge then to be Product

ProductEx Contains subtitle and a foreign key called ProductId

SELECT subtitle INTO Product
FROM Product t1 Full Outer Join Productex t2 On t1.ProductID =t2.ProductID

Is this correct?
0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 400 total points
ID: 34882476
seem like you want to bring content of subtitle from ProductEx into new table Product for each same ProductId

UPDATE Product
SET Product.subtitle = ProductEx.subtitle
FROM ProductEx
WHERE Product.ProductId = ProductEx.ProductId

Open in new window

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 34882855
Joe obviously has the solution.

But then you just wanted to update data, not create a table or a new field. subtitle already exists in Product and you just want to fill in new data.

If you really wanted to add a field to Product, this isn't possible with INTO your SQL would work if no table Prudct would exist it would create one, but ONLY with the subtitle field, that surely is not what you want.

When you initially said you wanted to merge two tables, I thought of merging table structures in the first place, not merging data.

Bye, Olaf.
0
 

Author Comment

by:Webbo_1980
ID: 34882955
Sorry Olaf it was a poor choce of words on my part
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question