Solved

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

Posted on 2011-02-13
5
236 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now