{"id":2597,"date":"2008-02-15T07:17:00","date_gmt":"2008-02-15T07:17:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/can-i-redeclare-a-variable-in-t-sql-teaser-for-sql-bits\/"},"modified":"2016-07-28T10:49:15","modified_gmt":"2016-07-28T10:49:15","slug":"can-i-redeclare-a-variable-in-t-sql-teaser-for-sql-bits","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/can-i-redeclare-a-variable-in-t-sql-teaser-for-sql-bits\/","title":{"rendered":"Can I redeclare a variable in T-SQL? Teaser for SQL Bits"},"content":{"rendered":"<p class=\"MsoNormal\">Variables in T-SQL behave differently from what people may be used to in languages like C or C#. The scope of a variable is the batch in which it is declared. This may not be intuitive to people who are used to local variables. So let&#8217;s look at an interesting example:<\/p>\n<p class=\"MsoNormal\">DECLARE @counter INT<\/p>\n<p class=\"MsoNormal\">SET @counter = 0<\/p>\n<p class=\"MsoNormal\">WHILE @counter &lt; 100<\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160; BEGIN<\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; DECLARE @value INT<\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; SET @value = ISNULL(@value, 0) + 5<\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; SET @counter = @counter + 1<\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160; END<\/p>\n<p class=\"MsoNormal\">SELECT&#160; @value<\/p>\n<p class=\"MsoNormal\">In this example we have a simple loop. The loop counter is a variable @counter. What is interesting that inside the loop we have a variable declaraion for @variable. We assign it a value (if it was null we set it to 5, if it was not null, we increment it by 5). Does this make sense? How could I ever consider the previous value if I&#8217;ve just declared the variable? What is unusual for many developers, is that the declaration is evaluated only once. This means that in the second iteration of the loop the @value variable is already declared and set, so we are incrementing it by five in every subsequent iteration. <\/p>\n<p class=\"MsoNormal\">At the end of the loop we can access the @value variable (do not forget that the scope is the whole batch), and the value will be 500 (100 iterations, 100 times adding 5).<\/p>\n<p class=\"MsoNormal\">Variable declarations are evaluated at the time of parsing, and the scope is the whole batch. Because of this, you cannot have two declare statements for the same variable in a batch, even if the only one declaration is reachable in the code. For example, the following statement will result in a parsing error:<\/p>\n<p class=\"MsoNormal\">IF 1 = 1 <\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160; DECLARE @a INT<\/p>\n<p class=\"MsoNormal\">ELSE <\/p>\n<p class=\"MsoNormal\">&#160;&#160;&#160; DECLARE @a INT<\/p>\n<p class=\"MsoNormal\">The error is:<\/p>\n<p class=\"MsoNormal\">Msg 134, Level 15, State 1, Line 4<\/p>\n<p class=\"MsoNormal\">The variable name &#8216;@a&#8217; has already been declared. Variable names must be unique within a query batch or stored procedure.<\/p>\n<p class=\"MsoNormal\">If you would like to know more about how transient data, like the above variable, is handled in SQL Server, come to my session at <a href=\"http:\/\/www.sqlbits.com\/default.aspx\">SQL Bits<\/a> in Birmingham (UK) on 1 March, 2008. Like the previous SQL Bits event it is free. The last one was held in Reading half a year ago, and with 20 sessions and over 300 people attending it, it provides a unique opportunity to learn about SQL Server, as well as to meet your fellow DBAs and SQL developers in the UK. I will talk about transient data in SQL Server, and you can learn about what is stored in tempdb, how the tempdb can be a performance bottleneck, what the differences between temporary tables and table variables are, what is stored in the transaction log file and how its can size be kept under control.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables in T-SQL behave differently from what people may be used to in languages like C or C#. The scope of a variable is the batch in which it is declared. This may not be intuitive to people who are used to local variables. So let&#8217;s look at an interesting example: DECLARE @counter INT SET&#8230;&hellip;<\/p>\n","protected":false},"author":9298,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2597","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/9298"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2597"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2597\/revisions"}],"predecessor-version":[{"id":41601,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2597\/revisions\/41601"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2597"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}