Solved

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

Posted on 2011-02-13
5
235 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry Olaf it was a poor choce of words on my part
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 50
CREATE DATABASE ENCRYPTION KEY 1 40
Distinct values from two tables 14 16
Calculating Business Hours 18 58
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

11 Experts available now in Live!

Get 1:1 Help Now