Checking @@error and @@rowcount

  • Does the following error checking work or do both @@error and @@rowcount need to be first captured in a select statement:

    Select * from mytable

    if @@error <> 0 OR @@rowcount = 0 begin <error handling> end

    Thanks, ron

    brain suffering from snow glare

     

  • This is just an example that might change the results even at each service pack level, you should use the recommended practice:

    select * from mytable

    select @err= @@error, @cnt = @@rowcount

    if @err <> or @cnt = 0 ....

    That will work no matter what, even accross versions

    Cheers,


    * Noel

  • I think that SELECT @err = @@error will ALWAYS resolve to 0.  To truly capture the @@error SET @err = @@error and then you can do

    IF @err <> 0 OR (SELECT COUNT(*) FROM myTable) = 0

      BEGIN

     



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  •  >> I think that SELECT @err = @@error will ALWAYS resolve to 0<<

    Allow me to differ.

    You MUST use  Select @err = @@error, @cnt = @@rowcount because is the only way in which you can read both global variables at once!

    if the statement previous to this does not cause an error then of course you will get @err equal to 0. You can't use SET in that case because it won't allow for multiple assignments

    Cheers,


    * Noel

  • Agreed.

    This is one area where SET can't be used, but you must use SELECT in order to catch both variables at once.

    There is simply no other way

    /Kenneth

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply