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 |
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2005-02-23 : 13:06:33
|
How can I tell if an update for an item occurred?Currently in my s'procs I use the following :IF(@@ERROR <> 0 OR @@ROWCOUNT = 0) RAISERROR('Failed to update item or record doesn't exist',16,1) But this doesn't seem to be a good test. I executed a stored proc on an item that doesn't exist and no error message was sent back!Any thoughts?Mike B |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-02-23 : 13:33:05
|
Well a sproc with that in it wouldn't even compileAnd I would usually separate those 2 error types outUSE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Col1 int IDENTITY(1,1), Col2 char(1))GOINSERT INTO myTable99(Col2)SELECT 'a' UNION ALLSELECT 'b' UNION ALLSELECT 'c'GOCREATE PROC mySproc99 (@Col1 int, @Col2 char(1))ASUPDATE myTable99 SET Col2 = @Col2 WHERE Col1 = @Col1IF(@@ERROR <> 0 OR @@ROWCOUNT = 0) RAISERROR('Failed to update item or record doesnt exist',16,1)ReturnGOEXEC mySproc99 1,'x'EXEC mySproc99 4,'x'GOSET NOCOUNT OFFDROP PROC mySproc99DROP TABLE myTable99GO Have a look athttp://weblogs.sqlteam.com/brettk/archive/2004/05/25/1378.aspxBrett8-) |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2005-02-23 : 13:36:26
|
quote: Originally posted by X002548 Well a sproc with that in it wouldn't even compileAnd I would usually separate those 2 error types outUSE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Col1 int IDENTITY(1,1), Col2 char(1))GOINSERT INTO myTable99(Col2)SELECT 'a' UNION ALLSELECT 'b' UNION ALLSELECT 'c'GOCREATE PROC mySproc99 (@Col1 int, @Col2 char(1))ASUPDATE myTable99 SET Col2 = @Col2 WHERE Col1 = @Col1IF(@@ERROR <> 0 OR @@ROWCOUNT = 0) RAISERROR('Failed to update item or record doesnt exist',16,1)ReturnGOEXEC mySproc99 1,'x'EXEC mySproc99 4,'x'GOSET NOCOUNT OFFDROP PROC mySproc99DROP TABLE myTable99GO Have a look athttp://weblogs.sqlteam.com/brettk/archive/2004/05/25/1378.aspxBrett8-)
Why wouldn't it compile? Not sure why you say this?Mike B |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-02-23 : 13:46:57
|
Your apostrophe will cause it to failIF(@@ERROR <> 0 OR @@ROWCOUNT = 0) RAISERROR('Failed to update item or record doesn't exist',16,1)Brett8-) |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2005-02-23 : 14:15:57
|
quote: Originally posted by X002548 Your apostrophe will cause it to failIF(@@ERROR <> 0 OR @@ROWCOUNT = 0) RAISERROR('Failed to update item or record doesn't exist',16,1)Brett8-)
LOL, thanks, didn't even notice that! Mike B :) |
|
|
|
|
|
|
|