Try Catch

  • I have declared some variables on the top then I have try catch. Then after the catch I have an insert statement to log into a logTable.

    Why the variable I declared before the begin try is not accessible after the end catch?

    alter procedure [dbo].[usp_sendmail]

    (

    @user varchar(100),

    @runDate datetime,

    @body varchar(500)

    set @user=system_user

    set @runDate = getdate()

    set @body = @user + @runDate + 'record has been updated.'

    begin try

    update statment

    end try

    begin catch

    end catch

    -- now keep a log of send email

    another insert statement with variables declared above

    )

  • I'd have to see more of your proc to know what's going on.

    I just ran this, and it had no problem accessing the variable:

    create proc dbo.TryCatch

    as

    set nocount on;

    declare @X int;

    select @X = 1;

    begin try;

    raiserror('Error', 16, 1);

    end try

    begin catch;

    print 'There was an error';

    end catch;

    select @X;

    go

    exec dbo.TryCatch;

    go

    drop proc dbo.TryCatch;

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 2 posts - 1 through 1 (of 1 total)

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