Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pras2007
Posting Yak Master
216 Posts |
Posted - 2009-10-06 : 13:59:27
|
| Hello All,I have an Update statment that update the current_Value table setting vaule = 0, if record_id is not found in the pervious_Value table. Also update value = 1 where record_id from table_a and table_B are equal. Now I need an insert statement that inserts records that is not found in the current_value table but exsist in the pervoius_value table and changing the sign on the amount field if not zero. In example of the tables and result table is shown below:Pervious_Value Record_ID First_Name Amount Value Difference1 Mike 50 25 NULL2 Sue 100 45 NULL3 Chris 75 15 NULL4 Tom 115 125 NULL9 Cliff 20 20 NULL Current_Value Record_ID First_Name Amount Value Difference5 Brent 5 25 NULL6 Kim 30 30 NULL1 Mike 50 25 NULL2 Sue 100 45 NULL3 Chris 75 15 NULL4 Tom 115 125 NULL Results Record_ID First_Name Amount Value Difference5 Brent 5 0 NULL6 Kim 30 0 NULL1 Mike 50 1 NULL2 Sue 100 1 NULL3 Chris 75 1 NULL4 Tom 115 1 NULL9 Cliff 20 -20' NULLUPDATE bSET b.Value=(CASE WHEN a.Record_ID IS NULL THEN 0 ELSE 1 END )FROM Current_Value bLEFT JOIN pervious_Value aON a.Record_ID=b.Record_ID |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-06 : 14:46:08
|
| sql only allows for one operation per statement. Either (select, insert, update, or delete). So Updates and Inserts needs to be seperate statements.Be One with the OptimizerTG |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-10-06 : 15:59:59
|
quote: Originally posted by TG sql only allows for one operation per statement. Either (select, insert, update, or delete). So Updates and Inserts needs to be seperate statements.Be One with the OptimizerTG
not really, check MERGE and OUTPUT INTO |
 |
|
|
dhw
Constraint Violating Yak Guru
332 Posts |
Posted - 2009-10-06 : 16:38:52
|
| Yes, the OUTPUT is something I recently discovered and it is great.Could you also use a Trigger? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-06 : 16:45:21
|
Thanks - I stand corrected I have yet to implement a MERGE solution - how's the performance vs. update/insert? I would guess that even though the user code is one statement it ends up being resolved into seperate ops under the sheets - but that is just a guess...Be One with the OptimizerTG |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-10-06 : 16:54:26
|
| even I havn't tried :) my company doesn't want to invest in 2008 yet. |
 |
|
|
smarty
Starting Member
13 Posts |
Posted - 2009-10-07 : 06:39:21
|
quote: Originally posted by TG Thanks - I stand corrected I have yet to implement a MERGE solution - how's the performance vs. update/insert? I would guess that even though the user code is one statement it ends up being resolved into seperate ops under the sheets - but that is just a guess...Be One with the OptimizerTG
The MERGE command is SQL2008 only-----------------------------------Free SQL server monitoring for DBA'swww.realsmartsoftware.co.uk |
 |
|
|
|
|
|