{"id":138,"date":"2019-09-24T07:49:42","date_gmt":"2019-09-24T07:49:42","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=138"},"modified":"2019-09-24T08:03:47","modified_gmt":"2019-09-24T08:03:47","slug":"variables","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"\n<h2>What is a&nbsp;variable<\/h2>\n\n\n\n<p>A variable is an object that holds a single value of a specific type e.g.,&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-int\/\">integer<\/a>,&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-date\/\">date<\/a>, or&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-varchar\/\">varying character string<\/a>.<\/p>\n\n\n\n<p>We typically use variables in the following cases:<\/p>\n\n\n\n<ul><li>As a loop counter to count the number of times a loop is performed.<\/li><li>To hold a value to be tested by a control-of-flow statement such as&nbsp;<code>WHILE<\/code>.<\/li><li>To store the value returned by a&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a>&nbsp;or a function<\/li><\/ul>\n\n\n\n<h2>Declaring a variable<\/h2>\n\n\n\n<p>To declare a variable, you use the&nbsp;<code>DECLARE<\/code>&nbsp;statement. For example, the following statement declares a variable named&nbsp;<code>@model_year<\/code>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DECLARE @model_year SMALLINT;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The&nbsp;<code>DECLARE<\/code>&nbsp;statement initializes a variable by assigning it a name and a data type. The variable name must start with the&nbsp;<code>@<\/code>&nbsp;sign. In this example, the data type of the&nbsp;<code>@model_year<\/code>&nbsp;variable is&nbsp;<code>SMALLINT<\/code>.<\/p>\n\n\n\n<p>By default, when a variable is declared, its value is set to&nbsp;<code>NULL<\/code>.<\/p>\n\n\n\n<p>Between the variable name and data type, you can use the optional&nbsp;<code>AS<\/code>&nbsp;keyword as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DECLARE @model_year AS SMALLINT;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To declare multiple variables, you separate variables by commas:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>DECLARE @model_year SMALLINT, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product_name VARCHAR(MAX);<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>Assigning a value to a variable<\/h2>\n\n\n\n<p>To assign a value to a variable, you use the&nbsp;<code>SET<\/code>&nbsp;statement. For example, the following statement assigns&nbsp;<code>2018<\/code>&nbsp;to the&nbsp;<code>@model_year<\/code>&nbsp;variable:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>SET @model_year = 2018;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>Using variables in a query<\/h2>\n\n\n\n<p>The following&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement uses the&nbsp;<code>@model_year<\/code>&nbsp;variable in the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code>&nbsp;clause to find the products of a specific model year:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910<\/td><td>SELECT&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;model_year,&nbsp;&nbsp;&nbsp;&nbsp;list_price FROM &nbsp;&nbsp;&nbsp;&nbsp;production.productsWHERE &nbsp;&nbsp;&nbsp;&nbsp;model_year = @model_yearORDER BY&nbsp;&nbsp;&nbsp;&nbsp;product_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Now, you can put everything together and execute the following code block to get a list of products whose model year is 2018:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011121314<\/td><td>DECLARE @model_year SMALLINT;&nbsp;SET @model_year = 2018;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;model_year,&nbsp;&nbsp;&nbsp;&nbsp;list_price FROM &nbsp;&nbsp;&nbsp;&nbsp;production.productsWHERE &nbsp;&nbsp;&nbsp;&nbsp;model_year = @model_yearORDER BY&nbsp;&nbsp;&nbsp;&nbsp;product_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Note that to execute the code, you click the Execute button as shown in the following picture:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png\" alt=\"Stored Procedure Variables - execute a code block\" class=\"wp-image-803\"\/><\/figure>\n\n\n\n<p>The following picture shows the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-output.png\" alt=\"Stored Procedure Variables - output\" class=\"wp-image-804\"\/><\/figure>\n\n\n\n<h2>Storing query result in a variable<\/h2>\n\n\n\n<p>The following steps describe how to store the query result in a variable:<\/p>\n\n\n\n<p>First, declare a variable named&nbsp;<code>@product_count<\/code>&nbsp;with the integer data type:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DECLARE @product_count INT;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Second, use the&nbsp;<code>SET<\/code>&nbsp;statement to assign the query\u2019s result set to the variable:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456<\/td><td>SET @product_count = (&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COUNT(*) &nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products );<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Third, output the content of the&nbsp;<code>@product_count<\/code>&nbsp;variable:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>SELECT @product_count;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Or you can use the&nbsp;<code>PRINT<\/code>&nbsp;statement to print out the content of a variable:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>PRINT @product_count;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>or<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>PRINT &#8216;The number of products is &#8216; + CAST(@product_count AS VARCHAR(MAX));<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The output in the messages tab is as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>The number of products is 204<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To hide the number of rows affected messages, you use the following statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>SET NOCOUNT ON;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>Selecting a record into variables<\/h2>\n\n\n\n<p>The following steps illustrate how to declare two variables, assign a record to them, and output the contents of the variables:<\/p>\n\n\n\n<p>First, declare variables that hold the product name and list price:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>DECLARE &nbsp;&nbsp;&nbsp;&nbsp;@product_name VARCHAR(MAX),&nbsp;&nbsp;&nbsp;&nbsp;@list_price DECIMAL(10,2);<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Second, assign the column names to the corresponding variables:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>SELECT &nbsp;&nbsp;&nbsp;&nbsp;@product_name = product_name,&nbsp;&nbsp;&nbsp;&nbsp;@list_price = list_priceFROM&nbsp;&nbsp;&nbsp;&nbsp;production.productsWHERE&nbsp;&nbsp;&nbsp;&nbsp;product_id = 100;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Third, output the content of the variables:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>SELECT &nbsp;&nbsp;&nbsp;&nbsp;@product_name AS product_name, &nbsp;&nbsp;&nbsp;&nbsp;@list_price AS list_price;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-assign-a-record-to-a-variable.png\" alt=\"Stored Procedure Variables - assign a record to a variable\" class=\"wp-image-805\"\/><\/figure>\n\n\n\n<h2>Accumulating values into a variable<\/h2>\n\n\n\n<p>The following stored procedure takes one parameter and returns a list of products as a string:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011121314151617181920<\/td><td>CREATE&nbsp;&nbsp;PROC uspGetProductList(&nbsp;&nbsp;&nbsp;&nbsp;@model_year SMALLINT) AS BEGIN&nbsp;&nbsp;&nbsp;&nbsp;DECLARE @product_list VARCHAR(MAX);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET @product_list = &#8221;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product_list = @product_list + product_name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ CHAR(10)&nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model_year = @model_year&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT @product_list;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this stored procedure:<\/p>\n\n\n\n<ul><li>First, we declared a variable named&nbsp;<code>@product_list<\/code>&nbsp;with varying character string type and set its value to blank.<\/li><li>Second, we selected the product name list from the products table based on the input&nbsp;<code>@model_year<\/code>. In the select list, we accumulated the product names to the&nbsp;<code>@product_list<\/code>&nbsp;variable. Note that the&nbsp;<code>CHAR(10)<\/code>&nbsp;returns the line feed character.<\/li><li>Third, we used the&nbsp;<code>PRINT<\/code>&nbsp;statement to print out the product list.<\/li><\/ul>\n\n\n\n<p>The following statement executes the&nbsp;<code>uspGetProductList<\/code>&nbsp;stored procedure:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXEC uspGetProductList 2018<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-Stored-Procedure-Example.png\" alt=\"Stored Procedure Variables - Stored Procedure Example\" class=\"wp-image-806\"\/><\/figure>\n\n\n\n<p>In this tutorial, you have learned about variables including declaring variables, setting their values, and assigning value fields of a record to the variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a&nbsp;variable A variable is an object that holds a single value of a specific type e.g.,&nbsp;integer,&nbsp;date, or&nbsp;varying character string. We typically use variables in the following cases: As a loop counter to count&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/138"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":139,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/139"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}