Author |
Topic |
ronin2307
Posting Yak Master
126 Posts |
Posted - 2007-07-23 : 14:09:46
|
I am trying to do the following but I am not sure it is possible:IF(try INSERT statement)Else 'if insert returns an error(run UPDATE statement)how do i write this? the error would be violation of the primary key constraintThanx |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-23 : 14:14:25
|
[code]IF NOT EXISTS( SELECT * FROM yourTable WHERE <Condition>) BEGIN INSERT INTO ... ENdELSe BEGIN UPDATE Table SET ... END[/code]Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2007-07-23 : 14:18:47
|
simple enough... thanx |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2007-07-23 : 14:32:12
|
what am i missingIF NOT EXISTS (Select * from bom_steel where fpart = @fpart) BEGIN INSERT INTO bom_steel VALUES (@fPart,@fL,@fH,@fW,@fDesc) SET ELSE BEGIN UPDATE bom_steel SET fl=@fl, fh=@fH, fW=@fW where fpart=@fpart SET Msg 156, Level 15, State 1, Procedure bom_UpIn, Line 25Incorrect syntax near the keyword 'ELSE'.Msg 170, Level 15, State 1, Procedure bom_UpIn, Line 31Line 31: Incorrect syntax near 'END'. |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-23 : 14:34:30
|
You need to END the BEGIN not SET it.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-23 : 14:35:11
|
quote: Originally posted by ronin2307 what am i missingIF NOT EXISTS (Select * from bom_steel where fpart = @fpart) BEGIN INSERT INTO bom_steel VALUES (@fPart,@fL,@fH,@fW,@fDesc) SET END ELSE BEGIN UPDATE bom_steel SET fl=@fl, fh=@fH, fW=@fW where fpart=@fpart SET END Msg 156, Level 15, State 1, Procedure bom_UpIn, Line 25Incorrect syntax near the keyword 'ELSE'.Msg 170, Level 15, State 1, Procedure bom_UpIn, Line 31Line 31: Incorrect syntax near 'END'.
Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2007-07-23 : 14:37:43
|
stupid....:-(thanx |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-07-23 : 14:37:44
|
[code]IF NOT EXISTS (Select * from bom_steel where fpart = @fpart) BEGIN INSERT INTO bom_steel VALUES (@fPart,@fL,@fH,@fW,@fDesc) SETEND ELSE BEGIN UPDATE bom_steel SET fl=@fl, fh=@fH, fW=@fW where fpart=@fpart SETEND[/code]Alteratly you can use this syntax:[code]UPDATE bom_steel SET fl=@fl, fh=@fH, fW=@fW where fpart=@fpartIF @@ROWCOUNT = 0BEGIN INSERT INTO bom_steel VALUES (@fPart,@fL,@fH,@fW,@fDesc) SET @fPart = SCOPE_IDENTITY()END[/code] |
 |
|
lucibra881
Starting Member
1 Post |
Posted - 2010-01-18 : 04:26:24
|
somebody can tell me what i'm missingThe error message is : *(Msg 156, Level 15, State 1, Procedure sp_UpdateTest, Line 29 Incorrect syntax near the keyword 'END'.)IF not exists(select Num from TABLENAME where Num = @Num) and exists(select Num from banks where Num = @Num)BEGIN insert into TABLENAME (adr_num)END*ELSEBEGIN delete from TABLENAME where Num = @NumENDLuci |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-01-18 : 04:41:47
|
insert into TABLENAME (adr_num) VALUES(...)orinsert into TABLENAME (adr_num) SELECT adr_num FROM ... |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-01-18 : 05:22:55
|
Also you dont need * after ENDMadhivananFailing to plan is Planning to fail |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-01-18 : 05:47:12
|
I assumed that was to indicate that was Line 29 - maybe not though! |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-01-18 : 05:50:53
|
quote: Originally posted by Kristen I assumed that was to indicate that was Line 29 - maybe not though!
May be MadhivananFailing to plan is Planning to fail |
 |
|
|