Stored proc exit

  • Hello,

    I am new to tsql coding and I am in the process of adding a piece of code to an already existing stored proc. Here is my code

    select @Count1=count(*) from [test] where [testcol] = 10

    select @Count2=count(*) from [test1] where [testcol] = 10

    if @Count1>0 or @Count2>0

    then exit the storedproc and dont process any further code in the stored proc. If only the condition fails above the stored proc shd be process. How do I achieve this?

    Thanks

  • It's really easy

    select @Count1=count(*) from [test] where [testcol] = 10

    select @Count2=count(*) from [test1] where [testcol] = 10

    IF @Count1<=0 AND @Count2<=0

    BEGIN

    -- Your SP code here

    END

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • DBA24 (6/11/2013)


    Hello,

    I am new to tsql coding and I am in the process of adding a piece of code to an already existing stored proc. Here is my code

    select @Count1=count(*) from [test] where [testcol] = 10

    select @Count2=count(*) from [test1] where [testcol] = 10

    if @Count1>0 or @Count2>0

    then exit the storedproc and dont process any further code in the stored proc. If only the condition fails above the stored proc shd be process. How do I achieve this?

    Thanks

    Maybe something like this:

    select @Count1=count(*) from [test] where [testcol] = 10

    select @Count2=count(*) from [test1] where [testcol] = 10

    if @Count1>0 or @Count2>0

    begin

    return

    end

    else

    begin

    -- carry on

    end

  • errr.. actually you want luis's given your requirements.

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

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