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 |
|
Lionheart
Starting Member
41 Posts |
Posted - 2009-08-26 : 10:43:05
|
| I have a table which stores data for a particular analysis by quarter. WHat I would like to do is add something to my stored procedure that will check to see if there is already any rows with that quarters name e.g. '209', delete these rows, and then replace them with the new results set.Can anybody suggest any code I can add to do this?Example...Quarter Loss Date109 100 12/31/08209 75 2/12/09I would like for the code to see that there is already the 209 loss, delete this, and then replace with the new 209 loss figures, but leave the rest of the table intact.Thanks,LH |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-08-26 : 13:33:35
|
| How about update instead of delete?http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-08-27 : 10:45:51
|
quote: Originally posted by Lionheart I have a table which stores data for a particular analysis by quarter. WHat I would like to do is add something to my stored procedure that will check to see if there is already any rows with that quarters name e.g. '209', delete these rows, and then replace them with the new results set.Can anybody suggest any code I can add to do this?Example...Quarter Loss Date109 100 12/31/08209 75 2/12/09I would like for the code to see that there is already the 209 loss, delete this, and then replace with the new 209 loss figures, but leave the rest of the table intact.Thanks,LH
you can simply add a check like thisIF EXISTS (SELECT 1 FROM table WHERE Quarter=@quarter)BEGINUPDATE tSET t.Loss=@loss,t.Date=@dateFROM table tWHERE t.quarter=@quarterENDELSEBEGININSERT INTO TableVALUES (@quarter,@loss,@date)ENDGO |
 |
|
|
|
|
|
|
|