exception handling in sqlserver

  • hi all

    i would like to know how to do exception handling in sqlserver

    thanx

    Rajiv.

  • You are asking an extremely general question. 

    You may want to hit some sights to find some good books on the subject. 

    Until then, you can look in BOL under RAISERROR and @@ERROR to get started...

    I wasn't born stupid - I had to study.

  • You will have to check for errors after every statement (that migth break your code)

    Here is an example that works for most cases:

    Update SomeTable Where Key = @Param

    Select @Err = @@ERROR, @Rowc = @@ROWCOUNT

    If @Err <> 0

       Goto ExitProcWithFailure  

    Else If @Rowc = 0

       Begin

             Set @ErrMsg = 'No record found'

             Goto ExitProcWithFailure  

       End

    -- Exit handler

    ExitProcWithSuccess:

    Return 0 -- Exit procedure

    -- Error handler

    ExitProcWithFailure:

    Raiserror('SP failed: %s', 16, 1, @ErrMsg) -- This MUST be the last statement

     

  • hey chrismi and farrell

    thanx for ur help. i got ur point chrismi.

    but suppose i try to insert a null value into a column (that is not supposed to contain null value) from asp.net.......there would b some error message in sqlserver. how should i catch those and tell the user at asp.net not to insert null values or to say that there is some sql server errors rather than i get a run time errors....hope u all understood my pblm

     

    thanx

    Rajiv.

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

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