Re: Syntax or IF statement

  • run this in Query analyser and you'll see the problem

    Select top 1 * from dbo.SysObjects

    Print @@Rowcount

    --1

    print @@Rowcount

    --0

    The rowcount variable is reupdated after the print statement. The 2nd time you call it its value is 0, therefor always going in the else.

    The workaround :

    Declare @MyRowCount as int

    set @MyRowcount = @@Rowcount

    if @MyRowcount > 0 --one or more FI sheet exist

    Begin

    IF @MyRowcount = 1

    return(1);

    Else

    return(2);

    end

    else

    --unable to locate FI for the work order

    return(0);

    End

    also this line would be much safer written like this :

    if @insert_lotnum = null or @insert_lotnum = ""

    if ISNULL(@insert_lotnum, '') = ''

    this is because null=null returns unknown which is never true nor false.

  • Hello Remi,

    Thanks for the code and explanations.

    I have two questions

    If the @@rowcount variable gets resetted to 0 shouldn't the return value be 0?what puzzles me is that it keeps on return 2 instead of 1 when there is just one record.

    Second, if I set  @MyRowcount = @@Rowcount

    Wouldn't @MyRowcount reset to 0 also?

    Thanks so..so much!

     

    if @MyRowcount > 0 --one or more FI sheet exist

    Begin

    IF @MyRowcount = 1

    return(1);

    Else

    return(2);

    else

    --unable to locate FI for the work order

    return(0);

    End

  • no because when it is first evaluated, it is greater than 0, the value is reset after the if is evaluated.

    @MyRowcount is a different variable kept in another part of the ram and is in no way related to @@rowcount besides the fact that it is populated with its value.

  • Thanks Remi!

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

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