Returning parameters and variables in SPs

  • Hi all,

    I have successfully written a couple of SPs that take input and output parameters.

    What I need to do now is to write some conditional logic within the SP based on the returned row count of a statement within the SP;

    UPDATE tableA WHERE colA ='x' and colB = 1

    If rows affected > 0 then, ELSE

    If there is one row affected then the SP continues and does the work (its basically locking a row before editing) and returns a success notice. If not it stops and returns a fail notice.

    I am not sure how to achieve this logic step based on the returned affected row count of the initial statement.

    Thanks for any help.

    Rolf

  • You can use @@rowcount to determine the number of rows affected by update statement.

    You might want to put the value of @@rowcount into another variable as it may change with each step in the stored procedure:

    declare @local_rowcount int

    update tableA

    where colA ='x' and colB = 1

    set @local_rowcount = @@rowcount

    if @local_rowcount = 1 begin

    /* various statements */

    end

    else begin

    /* Other statements */

    end

    Jeremy

  • Thanks,

    I didnt realise the logic would be so similar to VB etc.

    Looks simple enough..I'm off to try it thanks again.

    Rolf

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

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