Saturday, February 25, 2012

Really easy/dumb SQL question...

Pardon me, my SQL knowledge is not advanced enough to know how to do this, though I'm sure it's pretty simple:

Let's say I have a table called products, with fields like SKU, name, price. And let's say I have a temporary table with changes to be made (updates) to the current products.. same fields, SKU, name, price. I basically would like to be able to update currently existing entries in the products table, with the changes shown in the temporary table. Example:

PRODUCTS:
T231,Crazy Stick,4.99
023J87,Basketball Hoop,12.99
GB-572,CD Rack,8.99

TEMP. TABLE:
GB-572,Wooden CD Rack,8.99
T231,Crazy Stick,3.95

So I'd like to just merge the products in temp table w/ the ones in products. How can I do this?

Thanks!INSERT INTO PRODUCTS (SKU, name, price, etc)
SELECT SKU, name, price, etc
FROM #TEMP_TABLE|||But wouldn't that insert them as new items, ignoring the current entries? I'd like to update the existing entries in the Products table...|||Oh... well you need to do an UPDATE statement then. Naturally there has to be some sort of common key involved. I assumed you needed an INSERT statement as there was no common key.

General syntax:

UPDATE yourTable
SET yourTable.yourField = #temp.yourField, yourTable.field2 = #temp.field2
FROM yourTable INNER JOIN #temp on yourTable.key=#temp.key|||Ahhh.. thanks a ton!!

No comments:

Post a Comment