• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

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

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
Webbo_1980
Asked:
Webbo_1980
  • 2
  • 2
2 Solutions
 
Olaf DoschkeSoftware DeveloperCommented:
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
 
Webbo_1980Author Commented:
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
 
JoeNuvoCommented:
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
 
Olaf DoschkeSoftware DeveloperCommented:
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
 
Webbo_1980Author Commented:
Sorry Olaf it was a poor choce of words on my part
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now