﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Carlo Romagnano  / SET ROWCOUNT and table variable / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 04:19:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Good question and the last name will not be reached.</description><pubDate>Mon, 19 Mar 2012 10:45:39 GMT</pubDate><dc:creator>zymos</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Should have listened to my gut instinct on reading "[i]do not run it on production server[/i]" :-)</description><pubDate>Tue, 21 Sep 2010 10:27:37 GMT</pubDate><dc:creator>skrilla99</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>I'll parrot the thanks.  Good question and good examples which I definitely needed.-Dan B</description><pubDate>Tue, 21 Sep 2010 10:25:02 GMT</pubDate><dc:creator>skrilla99</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Hugo Kornelis (8/26/2010)[/b][hr][quote][b]da-zero (8/26/2010)[/b][hr]I do not really understand how the scope of the type variable works. It seems you can define it over and over again, without an error being thrown?[/quote]and[quote][b]martin.whitton (8/26/2010)[/b][hr]It's interesting that a variable can be declared multiple times if it's within a "while" loop (provided that it's only declared once in each iteration). When I first read the question I thought that declaring @a within the loop would cause an error on the second iteration.[/quote]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.[code="sql"]IF 1 = 2BEGIN;  PRINT 'Skip this';  DECLARE @a int;END;ELSEBEGIN;  SET @a = 1;END;SELECT @a;[/code]And this works as well, even though the part where the declaration sits is executed AFTER the assignment:[code="sql"]  GOTO Label2;Label1:  DECLARE @b int;  SELECT @b;  GOTO Label3;Label2:  SET @b = 3;  GOTO Label1;Label3:go[/code](Note that I do not endorse using GOTO in T-SQL code, nor deliberately writing spaghetti code for any other purposes than illustrating the difference between order of parsing and order of execution)[/quote]Kind of new for me - thanks - great explanation and examples</description><pubDate>Wed, 08 Sep 2010 09:08:42 GMT</pubDate><dc:creator>gchornenkyy</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>good question. Thanks!</description><pubDate>Wed, 01 Sep 2010 17:45:44 GMT</pubDate><dc:creator>Kangana Beri</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Carlo Romagnano (8/30/2010)[/b][hr][quote][b]nisan.al (8/29/2010)[/b][hr]I can't say i completely understand this issue.The while statement dont not stop because @i will never be equal to 1So how come the following select return 1 ?DECLARE @i floatset @i = 0set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2SELECT 1WHERE @i=1[/quote]As said above, the increment of 0.2 (precise) is different from 0.1 (approximate)[/quote]No, that is not the reason. Neither 0.1, nor 0.2 can be represented precisely in binary floating point.The binary equivalent of 0.2 is 0.000110001100011000110......The binary equivalent of 0.1 is 0.000011000110001100011......The real reason is that, with all rounding errors, you only see the effect if it stacks sufficiently. Because the calculation is done with some extra bits, most rounding errors remain invisible, especially becuase in random operations the rounding tend to even out each other. Repeating the same operation means repeating the same rounding error - and that will become visible when repeated often enough.[code="sql"]DECLARE @i float;set @i = 0;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;SELECT 1WHERE @i=1;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;set @i = @i + 0.2;SELECT 2WHERE @i=2;[/code]The first SELECT returns a value (as indicated by nisan.al), because the sum of the five rounding errors is so small that it has not yet reached the significant bits for the comparison. The second SELECT does not return a value, so obviously the sum of ten rounding errors does bring it into the significant bits.A good way to visualize this, is to check the corresponding rounding errors when using decimal notation. Calculate 1/with one decimal place (0.3). Add it three times (0.9). Compare it to the integer 1 (0.9 rounds to 1, so conclusion is: 1 = the sum of three (1/3)).Now add the same rounded 1/3 fifteen times more (5.4). Compare to the integer 6 (5.4 rounds to 5; not equal, so now the conclusion is that 6 &amp;lt;&amp;gt; the sum of eighteen (1/3)).The flloating point calculations obviously use lots more siginificant digits, but so do the comparisons; the problem remains the same.</description><pubDate>Mon, 30 Aug 2010 01:46:36 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]nisan.al (8/29/2010)[/b][hr]I can't say i completely understand this issue.The while statement dont not stop because @i will never be equal to 1So how come the following select return 1 ?DECLARE @i floatset @i = 0set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2SELECT 1WHERE @i=1[/quote]As said above, the increment of 0.2 (precise) is different from 0.1 (approximate)</description><pubDate>Mon, 30 Aug 2010 00:53:18 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>I can't say i completely understand this issue.The while statement dont not stop because @i will never be equal to 1So how come the following select return 1 ?DECLARE @i floatset @i = 0set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2set @i = @i + 0.2SELECT 1WHERE @i=1</description><pubDate>Sun, 29 Aug 2010 23:25:08 GMT</pubDate><dc:creator>nisan.al</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]mccork (8/25/2010)[/b][hr]The answer was almost obvious from the "[i]do not run it on production server[/i]" recommendation.&amp;lt;&amp;lt;snip&amp;gt;&amp;gt;[/quote]Agreed (and frankly, I wouldn't have chosen the right answer otherwise :-))!</description><pubDate>Fri, 27 Aug 2010 10:18:00 GMT</pubDate><dc:creator>Michael Poppers</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Thanks, Paul. That was the part that I missed that caused the misunderstanding on my part. I got in my head that we were starting @i at 0.9, but we're not. We're starting @i at 0 and incrementing by 0.1 which causes us to "miss" 1.0.</description><pubDate>Fri, 27 Aug 2010 08:11:22 GMT</pubDate><dc:creator>Steve Eckhart</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Steve Eckhart (8/27/2010)[/b][hr]Actually, we're doing the exact same thing here. This was the original query:[code="sql"]while @i &amp;lt;&amp;gt; 1begin  declare @a table(a int) set @rc = @i + 0.9 set rowcount @rc insert into @a select id from sysobjects set @i = @i + 0.1end[/code]So, there was only one floating point addition which made @i&amp;lt;&amp;gt;1 TRUE, but in the if statement @i&amp;lt;&amp;gt;1 is FALSE.[/quote]I don't see that. RC is set to @i + 0.9, then later 0.1 is added to @i. I'm not seeing the point where the value of @rc is copied into @i, which it would have to be for your statement to be correct. Maybe I'm just misreading the code?</description><pubDate>Fri, 27 Aug 2010 08:03:08 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Actually, we're doing the exact same thing here. This was the original query:[code="sql"]while @i &amp;lt;&amp;gt; 1begin  declare @a table(a int) set @rc = @i + 0.9 set rowcount @rc insert into @a select id from sysobjects set @i = @i + 0.1end[/code]So, there was only one floating point addition which made @i&amp;lt;&amp;gt;1 TRUE, but in the if statement @i&amp;lt;&amp;gt;1 is FALSE.</description><pubDate>Fri, 27 Aug 2010 07:55:07 GMT</pubDate><dc:creator>Steve Eckhart</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Gopinath Srirangan (8/27/2010)[/b][hr]Hi,But another sample query as shown below gives expected result which contradicts from while loop.DECLARE @i floatset @i = .9set @i = @i + .1[/quote]You're only adding one float there, so the cumulative error isn't that great. In the original WHILE loop there were ten separate additions of 0.1, which caused more of an issue.</description><pubDate>Fri, 27 Aug 2010 04:37:59 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Please try to see the trailing decimal digits using[code] print convert(decimal(38,36),@i) [/code] I think the 1st example behaved well because it was dealing with an even number (8), while the 2nd does not because it was with odd numbers (7).Because float comparison does not behave uniformly in all cases, I think this is why float comparison should not be used to test for equality, this is "equal", = and "not equal", &amp;lt;&amp;gt;.  But float may be used for less than or greater than, if accuracy/significance is required then the ideal less actual result may be used re: "while (ideal - actual) &amp;gt; 0.0001".  The latter assumes the operations will converge close to the ideal point, otherwise there must be additional codes to check if divergence is happening in order to put a stop operation.</description><pubDate>Fri, 27 Aug 2010 04:33:52 GMT</pubDate><dc:creator>Open Minded</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>following script doesn't go in infinite loopDECLARE @i float,@rc intset @i = 0while @i &amp;lt;&amp;gt; 1begin  	set @i = @i + 0.8	set @i = @i + 0.1	set @i = @i + 0.1	print @iendBut below script is going in infinite loopDECLARE @i float,@rc intset @i = 0while @i &amp;lt;&amp;gt; 1begin  	set @i = @i + 0.7	set @i = @i + 0.1	set @i = @i + 0.1	set @i = @i + 0.1	print @iendCan any one know the answer on this??RegardsManmohan</description><pubDate>Fri, 27 Aug 2010 04:13:55 GMT</pubDate><dc:creator>mm_sharma82</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Hello All,I'm not agree with explaination given."Floating point data is approximate (http://msdn.microsoft.com/en-us/library/ms173773.aspx). So an increment of 0.1 will never be equal to 1 and the exit condition @i &amp;lt;&amp;gt; 1 always is true (infinite loop). The script contains two bad mistakes: floating comparison and a WHILE condition that easily falls into infinite loop. "Following script is also increasing the variable @i, but it iterate loop once.DECLARE @i float,@rc intset @i = 0while @i &amp;lt;&amp;gt; 1begin  	declare @a table(a int) 	set @i = @i + 0.9	insert into @a 	select 1	set @i = @i + 0.1end-- LAST SELECTSELECT * FROM @a</description><pubDate>Fri, 27 Aug 2010 03:58:43 GMT</pubDate><dc:creator>mm_sharma82</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>weitzera and scott,Thank you for the explanations, I have a much better understanding of "float" approximation issues now</description><pubDate>Fri, 27 Aug 2010 03:53:59 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Hi,But another sample query as shown below gives expected result which contradicts from while loop.DECLARE @i floatset @i = .9set @i = @i + .1-- Query 1if @i &amp;lt;&amp;gt; 1print 'True'elseprint 'False'Answer: 'False'-- Query 2if @i = 1print 'True'elseprint 'False'Answer: 'True'Can anyone pls explain on difference in this two. (ie while loop resulted is true but if condition in query 1 returned 'False' </description><pubDate>Fri, 27 Aug 2010 03:51:03 GMT</pubDate><dc:creator>Gopi S</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]weitzera (8/26/2010)[/b][hr]for example 1, .9, .1, and .005 are representable just fine[/quote]The mantissa of a FLOAT is a 53-bit binary fraction, and it can exactly represent only rational numbers whose denominator is a power of 2. This means that 0.1 can't be represented exactly, the value stored is actually closer to 0.1000000000000000055511.  Neither can 0.9 or 0.005.  Any integer value under 15 digits long can be stored exactly, as can binary fractions like 0.5, 0.25, 0.125, etc.Floating-point calculations are done in the CPU with 80-bit precision and the result is rounded to 53 bits, which in many cases works very well and the approximation errors cancel out.  10 x 0.1 results in exactly 1.0, for example.  But repeatedly adding 0.1 means the same tiny error is added at each step and it eventually becomes noticable as shown in the examples posted above.</description><pubDate>Thu, 26 Aug 2010 15:11:01 GMT</pubDate><dc:creator>Scott Coleman</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Here's another little script to demonstrate the effect of using float.  Although the local variable looks pretty good out of a simple "print @i", converting the value to decimal bears out the internal inaccuracy.[code="sql"]declare @i floatset @i = .1 while @i &amp;lt;= 10.0begin	print @i	print convert(decimal(38,36),@i)	set @i = @i + .1End[/code]The first few rows returned are here:[code="plain"]0.10.1000000000000000000000000000000000000.20.2000000000000000000000000000000000000.30.3000000000000000600000000000000000000.40.4000000000000000000000000000000000000.50.5000000000000000000000000000000000000.60.6000000000000000000000000000000000000.70.7000000000000000000000000000000000000.80.7999999999999998800000000000000000000.90.89999999999999988000000000000000000010.999999999999999880000000000000000000[/code] and the last few are here:[code="plain"]9.69.5999999999999819000000000000000000009.79.6999999999999815000000000000000000009.89.7999999999999812000000000000000000009.99.899999999999980800000000000000000000109.999999999999980400000000000000000000[/code]It's now obvious that 9.9999999999999804 is not equal to 10.</description><pubDate>Thu, 26 Aug 2010 14:02:22 GMT</pubDate><dc:creator>john.arnott</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Pete,No, by approximate, it is not meant that no finite numbers are representable, for example 1, .9, .1, and .005 are representable just fine.  It is just an acknowledgment that certain numbers are not representable in base two, just as some numbers are not representable in base 10In the example given, the problem is that .3 and .7 are not representable.  A simple algorithm for converting a base 10 fraction to a base 2 fraction is as follows:[code="plain"]set number = .1 (remember, this is binary, .1 = 2^-1, or 1/(2^1) or .5 in decimal)set exp = -1while number &amp;lt;&amp;gt; target    if number &amp;gt; target        set number = number - 2^exp    end if    set exp = exp - 1    set number = number + 2^expend while[/code]If you follow this for .3 you get:.01001100110011001100110011... ad nauseumCompare this to the process of converting .1 from base 3 into base 10 (.1 in base 3 is the ratio 1/3)A common question when this explanation is given is: 3 can be represented in base 2, and -1 can be represented, so why doesn't the floating point data type represent the value as "3 * 10^-1"  The answer as I understand it is that floats are highly optimized for storage space, so for the same number of bits, you would get less range of representable numbers from a float specified this way.  Feel free to correct me on this if there were other considerations I'm forgetting.</description><pubDate>Thu, 26 Aug 2010 10:42:51 GMT</pubDate><dc:creator>weitzera</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Steve Eckhart (8/26/2010)[/b][hr]Very good question. I discovered that whether or not 0.9 + 0.1 &amp;lt;&amp;gt; 1 is not consistent I guess depending on sequence. If you execute the following in SQL Server 2005, you get "Equal":[code="sql"]declare @f floatset @f = 0.9set @f = @f + 0.1if @f &amp;lt;&amp;gt; 1	print 'Not Equal'else	print 'Equal'[/code][/quote]try this one:declare @f floatset @f = 0set @f = @f + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1print 1 - @f</description><pubDate>Thu, 26 Aug 2010 08:43:38 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Very good question. I discovered that whether or not 0.9 + 0.1 &amp;lt;&amp;gt; 1 is not consistent I guess depending on sequence. If you execute the following in SQL Server 2005, you get "Equal":[code="sql"]declare @f floatset @f = 0.9set @f = @f + 0.1if @f &amp;lt;&amp;gt; 1	print 'Not Equal'else	print 'Equal'[/code]</description><pubDate>Thu, 26 Aug 2010 08:37:25 GMT</pubDate><dc:creator>Steve Eckhart</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]mccork (8/25/2010)[/b][hr]The answer was almost obvious from the "[i]do not run it on production server[/i]" recommendation.But, a good question for highlighting the pitfalls of "float".[/quote]That is what led me to guess correctly on this one, especially since that recommendation is literally one that goes without saying on SSC (no one should ever run QOTD code on production, even if they think that nothing can go wrong... Murphy's law and all that) - so I still need to read more about why this is. But I agree it is still a good question and leads to fruitful discussion about loops and floats.Thanks,webrunner</description><pubDate>Thu, 26 Aug 2010 08:21:11 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>The main point of the QOD about testing for equality of floating point data is an excellent one.  But, I admit I am more fascinated by this:[quote][b]Hugo Kornelis (8/26/2010)[/b][hr] 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. [/quote]I spent some time playing with Hugo's illustrations.  Whoa.  This seems like a fundamental thing of which I was entirely unaware.  :w00t:</description><pubDate>Thu, 26 Aug 2010 08:01:55 GMT</pubDate><dc:creator>Rose Bud</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Carlo Romagnano (8/26/2010)[/b][hr]To compare two floating point I use this syntax:IF ABS(varFloat1 - varFloat2) &amp;lt; 0.01 -- place here the precision you want   print 'varFloat1 = varFloat2'ELSE   print 'varFloat1 &amp;lt;&amp;gt; varFloat2'[/quote]Hey, I remember using that style in engineering-physics machine problems.</description><pubDate>Thu, 26 Aug 2010 07:21:00 GMT</pubDate><dc:creator>Open Minded</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Great question.  I learned a couple things about float....it is approximate, and I should never use it in my environment!  Thanks.</description><pubDate>Thu, 26 Aug 2010 07:05:26 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>To compare two floating point I use this syntax:IF ABS(varFloat1 - varFloat2) &amp;lt; 0.01 -- place here the precision you want   print 'varFloat1 = varFloat2'ELSE   print 'varFloat1 &amp;lt;&amp;gt; varFloat2'</description><pubDate>Thu, 26 Aug 2010 06:22:28 GMT</pubDate><dc:creator>Carlo Romagnano</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Great question Carlo and thank you Hugo for the great explanations.</description><pubDate>Thu, 26 Aug 2010 05:59:04 GMT</pubDate><dc:creator>Dennissinned</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>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!</description><pubDate>Thu, 26 Aug 2010 05:49:15 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Hugo Kornelis (8/26/2010)[/b][hr]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.[/quote]Thanks for the great explanation. I've learned two things now from this question, what a day :-D</description><pubDate>Thu, 26 Aug 2010 04:33:02 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>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.</description><pubDate>Thu, 26 Aug 2010 03:42:07 GMT</pubDate><dc:creator>Open Minded</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Well I never!</description><pubDate>Thu, 26 Aug 2010 03:36:07 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Toreador (8/26/2010)[/b][hr]We can't easily use 'top' because it won't accept a variable, ie [i]select top @i...[/i]won't work, so we'd need to use dynamic sql everywhere.[/quote]Top will accept a variable if it's in brackets, so[i]select top (@i)...[/i]will work</description><pubDate>Thu, 26 Aug 2010 03:32:48 GMT</pubDate><dc:creator>martin.whitton</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Thanks Hugo, for the well-thought out answers you often give to us.[quote][b]Hugo Kornelis (8/26/2010)[/b][hr]Last comment, and then I'll shut up (for now)[quote][b]Open Minded (8/26/2010)[/b][hr]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" ?[/quote]Yes. A test of "IF @float &amp;gt; 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) &amp;lt;&amp;gt; 1", the code finishes and returns a single value.[/quote]</description><pubDate>Thu, 26 Aug 2010 03:25:03 GMT</pubDate><dc:creator>Open Minded</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Yip, worrying indeed.Lets hope sanity prevails and they leave this feature alone</description><pubDate>Thu, 26 Aug 2010 03:15:26 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>Interesting/worrying! We have lots of code that does something like[i]declare @i intset @i = 10set rowcount @iinsert into tab1select * from tab2[/i]in order to insert the top [i]n[/i] rows from one table into another.We can't easily use 'top' because it won't accept a variable, ie [i]select top @i...[/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[i]set rowcount 1delete from table1[/i]will only delete 1 row.</description><pubDate>Thu, 26 Aug 2010 03:11:12 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>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 :)</description><pubDate>Thu, 26 Aug 2010 03:02:07 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>[quote][b]Pete Cox (8/26/2010)[/b][hr]BTW, I think ROWCOUNT stops having this effect in versions of SQL server later than 2005[/quote]Nope, still works in 2008 (luckily, or our code would break!)</description><pubDate>Thu, 26 Aug 2010 02:53:00 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: SET ROWCOUNT and table variable</title><link>http://www.sqlservercentral.com/Forums/Topic975310-1299-1.aspx</link><description>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</description><pubDate>Thu, 26 Aug 2010 02:50:55 GMT</pubDate><dc:creator>Pete Cox</dc:creator></item></channel></rss>