Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2007, 03:54 AM
Registered User
 
Join Date: May 2005
Location: Wellington, north island, New Zealand.
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!"
Reply With Quote
  #2 (permalink)  
Old August 7th, 2007, 05:38 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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 )

Reply With Quote
  #3 (permalink)  
Old August 7th, 2007, 06:29 AM
Friend of Wrox
Points: 1,536, Level: 15
Points: 1,536, Level: 15 Points: 1,536, Level: 15 Points: 1,536, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
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
Reply With Quote
  #4 (permalink)  
Old August 8th, 2007, 12:20 AM
Registered User
 
Join Date: May 2005
Location: Wellington, north island, New Zealand.
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!"
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:35 PM.


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