{"id":5763,"date":"2013-06-20T00:00:12","date_gmt":"2013-06-20T00:00:12","guid":{"rendered":"http:\/\/craftydba.com\/?p=5763"},"modified":"2016-04-22T12:48:45","modified_gmt":"2016-04-22T12:48:45","slug":"tsql-variables","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5763","title":{"rendered":"Using Variables"},"content":{"rendered":"<p>I am going to talk about Transaction SQL <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff848809.aspx\">variables<\/a>.  Any stored program that does complex business processing will use one of more of variables to craft an algorithm.<\/p>\n<p>During my college days, I had to take a couple physics classes.  <a href=\"http:\/\/en.wikipedia.org\/wiki\/Newton's_laws_of_motion\">Newton&#8217;s<\/a> second law states that force is equal to mass times acceleration.  <\/p>\n<p>The comic strip on the left shows how variables in the fourth picture can be used as place holders in a complex equation.  This concept is key for crafting very complex programs.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188927.aspx\">DECLARE<\/a> keyword is used in Transaction SQL (T-SQL) to define a variable.  Unlike some languages, all variables in T-SQL need to be defined before they are used. <\/p>\n<p>The example below declares a variable, stores text in the variable and shows the value of the variable. <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"set statement - using variables\">\r\n\r\n--\r\n-- My first program\r\n--\r\n\r\n-- Declare variable\r\nDECLARE @VAR_A VARCHAR(128);\r\n\r\n-- Set variable\r\nSET @VAR_A = 'Hello World';\r\n\r\n-- Show the variable\r\nPRINT @VAR_A;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<p>The output of this program is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nHello World\r\n<\/span><\/pre>\n<\/p>\n<p>The first way to store a value in a variable is to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189484.aspx\">SET<\/a> keyword as shown above.  <\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176047.aspx\">PRINT<\/a> keyword is used to display either a constant text string or a text variable.  The output of this statement is directed to the results or message window depending upon the output mode of the query in the SQL Server Management Studio (SSMS).  <\/p>\n<p>In short, this statement is a quick way to add debugging lines to your code during development.<\/p>\n<p>A second way to store a value in a variable is to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187330.aspx\">SELECT<\/a> keyword.  The example below redesigns the code above using this keyword.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"select statement - using variables\">\r\n\/*\r\n My second program\r\n*\/\r\n\r\n\/* Declare variable *\/\r\nDECLARE @VAR_B VARCHAR(128);\r\n\r\n\/* Another way to set *\/\r\nSELECT @VAR_B = 'Neo, the matrix has you!';\r\n\r\n\/* Show the variable *\/\r\nPRINT @VAR_B;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<p>The output of this program is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nNeo, the matrix has you!\r\n<\/pre>\n<\/p>\n<p>Please note that the first program is using a multi-line comment (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178623.aspx\">\/* *\/<\/a>) while the second program uses a single-line comment (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms181627.aspx\">&#8212;<\/a>).<\/p>\n<p>The above two programs are very simplistic in nature.  A more realistic piece of code is to list all the databases.  System databases to be exact.  This program shows how the DECLARE statement can be used to initialize a variable on the same line.  <\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188332.aspx\">EXEC<\/a> keyword executes the dynamic SQL in another session and returns the results to SSMS.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"dynamic tsql- using variables\">\r\n--\r\n-- My Third Program\r\n--\r\n\r\n-- Declare & set\r\nDECLARE @VAR_SQL VARCHAR(128) = \r\n    'select convert(varchar(10), name) as db_name, database_id as db_id \r\n     from sys.databases \r\n     where database_id < 5';\r\n\r\n\/* Run the statement *\/\r\nEXEC (@VAR_SQL);\r\n\r\n<\/pre>\n<\/p>\n<p>The output of this program is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\ndb_name    db_id\r\n---------- -----------\r\nmaster     1\r\ntempdb     2\r\nmodel      3\r\nmsdb       4\r\n\r\n<\/pre>\n<\/p>\n<p>To recap this talk, variables are a great way to store results between statements in a program.  Use the DECLARE keyword to define and optionally set your variable.  Both the SET and SELECT keywords can be used to assign a value to a variable.  <\/p>\n<p>Last but not least, please comment your program.  The next TSQL developer will thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to talk about Transaction SQL variables. Any stored program that does complex business processing will use one of more of variables to craft an algorithm. During my college days, I had to take a couple physics classes. Newton&#8217;s second law states that force is equal to mass times acceleration. The comic strip on the left shows how variables in the fourth picture can be used as place holders in a complex equation. This concept is key for crafting very complex programs. The DECLARE keyword is used in&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5788,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[862,31,224,431,15,323,37,331,29,863],"class_list":["post-5763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-db-dev","tag-comments","tag-database-developer","tag-declare","tag-exec","tag-john-f-miner-iii","tag-print","tag-select","tag-set","tag-tsql","tag-variables"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5763"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5763\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5788"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}