SET ROWCOUNT and table variable

  • Nice question, made me think about what "approximate" means.

    Is always true to say, as in the explanation, that because the value of float variable is approximate, it can never = 1?

    It must equal some value or other and may under some conditions = 1 perhaps? Or not ? Still don't know really.

    Or are we talking about the inapplicability of the equality operator ?

    BTW, I think ROWCOUNT stops having this effect in versions of SQL server later than 2005

  • Pete Cox (8/26/2010)


    BTW, I think ROWCOUNT stops having this effect in versions of SQL server later than 2005

    Nope, still works in 2008 (luckily, or our code would break!)

  • My Local BoL 2005 quote follows

    "Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in the next release of SQL Server. Avoid using SET ROWCOUNT together with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. Also, for DELETE, INSERT, and UPDATE statements that currently use SET ROWCOUNT, we recommend that you rewrite them to use the TOP syntax. For more information, see DELETE (Transact-SQL), INSERT (Transact-SQL), or UPDATE (Transact-SQL)."

    Then MSDN Online BoL has the same Quote for 2008.

    (http://msdn.microsoft.com/en-us/library/ms188774.aspx)

    So it looks like it "might" be removed in a later release, sometime, perhaps, maybe 🙂

  • Interesting/worrying! We have lots of code that does something like

    declare @i int

    set @i = 10

    set rowcount @i

    insert into tab1

    select * from tab2

    in order to insert the top n rows from one table into another.

    We can't easily use 'top' because it won't accept a variable, ie

    select top @i...

    won't work, so we'd need to use dynamic sql everywhere.

    I've verified that this use of rowcount still works in 2008, and for interest it works with delete as well, ie

    set rowcount 1

    delete from table1

    will only delete 1 row.

  • Yip, worrying indeed.

    Lets hope sanity prevails and they leave this feature alone

  • Thanks Hugo, for the well-thought out answers you often give to us.

    Hugo Kornelis (8/26/2010)


    Last comment, and then I'll shut up (for now)

    Open Minded (8/26/2010)


    isn't it possible to use approximation if one is not specific of the exact value of the approximation, like "less than" and "greater than" ?

    Yes. A test of "IF @float > 1.0" is okay.

    And if you really have to test for equality with floating point data, you have to consider how many of the decimals are relevant and then round: "IF ROUND(@float, 3) = 1.000" should work. If you change the WHILE in this question to "while round(@1,3) <> 1", the code finishes and returns a single value.

  • Toreador (8/26/2010)


    We can't easily use 'top' because it won't accept a variable, ie

    select top @i...

    won't work, so we'd need to use dynamic sql everywhere.

    Top will accept a variable if it's in brackets, so

    select top (@i)...

    will work

  • Well I never!

  • Yah, it works. I never thought (@variable) in select would work...

    Does this work in SQL 2000?

    I remember vaguely reaching a wall on that thus I had to hardcode upper limit. I will google now.

  • Hugo Kornelis (8/26/2010)


    The variable is actually declared once. Declarations are not relly executable statements. They are processed when a batch is parsed, not when it it executed. The declaration has to be positioned before the use in "left to right / top to bottom" order, not in execution order. That is why this works, even though the code path that containst the declaration is never executed.

    Thanks for the great explanation. I've learned two things now from this question, what a day 😀

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • I actually guessed the outcome as soon as I saw the comparison--but then, I was a programmer for ten years, and I think "DO NOT DIRECTLY COMPARE TWO FLOATING-POINT VALUES" is something you inevitably learn after a while! I confess I didn't notice that the @a was apparently declared in an unreachable bit of code, although Hugo's explanation for why that still works is interesting...guess SQL's non-procedural nature extends further than I thought!

  • Great question Carlo and thank you Hugo for the great explanations.

  • To compare two floating point I use this syntax:

    IF ABS(varFloat1 - varFloat2) < 0.01 -- place here the precision you want

    print 'varFloat1 = varFloat2'

    ELSE

    print 'varFloat1 <> varFloat2'

  • Great question. I learned a couple things about float....it is approximate, and I should never use it in my environment! Thanks.

  • Carlo Romagnano (8/26/2010)


    To compare two floating point I use this syntax:

    IF ABS(varFloat1 - varFloat2) < 0.01 -- place here the precision you want

    print 'varFloat1 = varFloat2'

    ELSE

    print 'varFloat1 <> varFloat2'

    Hey, I remember using that style in engineering-physics machine problems.

Viewing 15 posts - 16 through 30 (of 54 total)

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