Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-13
5
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 30

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 400 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 1600 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 30

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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