Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2007, 03:54 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to bigshot
Default Update querie

I have two tables with identical structure such as
ID, product1, product2, product3, product4, product5

Table 1 is filled with 5 ID numbers, say 1,2,3,4,5 and NULL (ie nothing) for the products.

Table 2 is filled with 10 ID numbers, 1,2,3,4,5,6,7,8,9,10 and all product details filled in (doesnt matter what with).

I have to update Table 1 such that it basically becomes a copy of table 2 with all the details.

The question basically here is can this be done with a single query or must you use an UPDATE query to first update the details of the ID numbers 1-5, and then an INSERT query to get the details of ID's 6-10 into table one?
I have tried the UPDATE query, and managed to get it to update with ID's 1-5 with the product details, but did not manage to get it to insert ID's 6-10 into table 1.


"It doesn't matter what you think!"
 
Old August 7th, 2007, 05:38 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi.

As far as I know, you need to use both update and insert.
For the insert you could try something like this:

INSERT INTO Table1(ID,product1,...)
SELECT ID, product1,...
FROM Table2 t2
WHERE NOT EXISTS
    (SELECT t1.ID FROM Table1 t1 WHERE t2.id = t1.id )

 
Old August 7th, 2007, 06:29 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Not trying to be a smart-a$$ about this, but why don't you normalize your tables so you don't need nearly duplicate data in two separate tables? Also, having multiple products in the same row for a given ID is a form of denormalization that you can probably do without.

Of course, you may not be able to make such changes to the tables...

--Jeff Moden
 
Old August 8th, 2007, 12:20 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to bigshot
Default

yeah this is just an exercise so its not about normalising, but thanks for the suggestions, and yeah thanks Gert, that works perfectly.

"It doesn't matter what you think!"





Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
Update FT BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 September 20th, 2007 12:34 AM
Update link doesn't update in FormsView shaly ASP.NET 2.0 Basics 0 December 6th, 2006 04:33 PM
Can't update!! comicghozt ASP.NET 1.0 and 1.1 Basics 7 June 26th, 2006 10:38 AM
Datagrid.update() and DataAdapter.Update aarunlal ASP.NET 2.0 Professional 2 February 23rd, 2006 11:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.