|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:21 AM
Points: 5,244,
Visits: 7,062
|
|
SanDroid (3/28/2011)
Hugo Kornelis (3/28/2011) I got it correct. I was surprised at first at the "SELECT @i+=1" syntax. I knew about the compount operators and have happily used "SET @i=1", but seeing never realised that it can equally well be used with SELECT. Thanks!I think setting variables with a SELECT statement like "SELECT @var1 = 1, @var2 =2" has always worked in SQL server. I have a pre SQL 92 SQL command refference with examples and it is listed there. It was also supported on MS SQL Server 7. Yes, it has. For as long as I can remember (and that includes some vague recollection of SQL Server 4.2). It just never occured to me that the += operator can also be used in a SELECT instead of only in a SET. My fault entirely - and I'm glad that this question showed me that this is possible as well. (Though I still prefer to use SET for setting variables).
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 10:45 AM
Points: 1,786,
Visits: 1,006
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 11:54 AM
Points: 7,112,
Visits: 7,188
|
|
Christian Buettner-167247 (3/28/2011)
Tom.Thomson (3/27/2011) One comment though: depending on settings of ARITHABORT and ANSI WARNINGS, it is quite possible that none of the answer options will result, because the batch is terminated before it reaches the select statement.Why should the batch be terminated? I don't see any issues here. If ARITHABORT is ON and ANSI WARNINGS off, arithmetic overlow causes the batch to be aborted. See the second sentence of the remarks section of this BoL page. SELECT @i+=1 will cause arithmetic overflow on the last iteration, attempting to set a tinyint to 256.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
Tom.Thomson (3/29/2011)
If ARITHABORT is ON and ANSI WARNINGS off, arithmetic overlow causes the batch to be aborted. See the second sentence of the remarks section of this BoL page. SELECT @i+=1 will cause arithmetic overflow on the last iteration, attempting to set a tinyint to 256.
Why would it do that, the WHILE clause:
while @i < 255 shouldn't run the SELECT statement to increment @i when @i is 255. So the last iteration is with @i=254 getting incremented to 255.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 11:54 AM
Points: 7,112,
Visits: 7,188
|
|
UMG Developer (3/29/2011)
Tom.Thomson (3/29/2011)
If ARITHABORT is ON and ANSI WARNINGS off, arithmetic overlow causes the batch to be aborted. See the second sentence of the remarks section of this BoL page. SELECT @i+=1 will cause arithmetic overflow on the last iteration, attempting to set a tinyint to 256. Why would it do that, the WHILE clause: while @i < 255 shouldn't run the SELECT statement to increment @i when @i is 255. So the last iteration is with @i=254 getting incremented to 255. O mo chreach! I must have gone crazy to read < as <=.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 7:51 AM
Points: 1,085,
Visits: 1,166
|
|
i read somewhere, this is a new feature in sql server 2008. That's why i got point.
Nice question.
Keep Learning - Keep Growing !!! http://growwithsql.blogspot.in
Thanks Vinay Kumar
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 9:43 AM
Points: 469,
Visits: 193
|
|
| Thank you for the good question, reviewing a feature that was introduced in SQL Server 2008 that did not exist in SQL Server 2005
|
|
|
|