{"id":802,"date":"2018-09-30T17:49:23","date_gmt":"2018-09-30T10:49:23","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=802"},"modified":"2020-04-11T20:13:13","modified_gmt":"2020-04-11T13:13:13","slug":"variables","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about variables including declaring variables, setting their values, and assigning value fields of a record to variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-avariable'>What is a&nbsp;variable <a href=\"#what-is-avariable\" class=\"anchor\" id=\"what-is-avariable\" title=\"Anchor for What is a&nbsp;variable\">#<\/a><\/h2>\n\n\n\n<p>A variable is an object that holds a single value of a specific type e.g., <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-int\/\">integer<\/a>, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-date\/\">date<\/a>, or <a href=\"https:\/\/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 class=\"wp-block-list\"><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 <code>WHILE<\/code>.<\/li><li>To store the value returned by a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a> or a function<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='declaring-a-variable'>Declaring a variable <a href=\"#declaring-a-variable\" class=\"anchor\" id=\"declaring-a-variable\" title=\"Anchor for Declaring a variable\">#<\/a><\/h2>\n\n\n\n<p>To declare a variable, you use the <code>DECLARE<\/code> statement. For example, the following statement declares a variable named <code>@model_year<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @model_year <span class=\"hljs-built_in\">SMALLINT<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>DECLARE<\/code> statement initializes a variable by assigning it a name and a data type. The variable name must start with the <code>@<\/code> sign. In this example, the data type of the <code>@model_year<\/code> variable is <code>SMALLINT<\/code>.<\/p>\n\n\n\n<p>By default, when a variable is declared, its value is set to <code>NULL<\/code>.<\/p>\n\n\n\n<p>Between the variable name and data type, you can use the optional <code>AS<\/code> keyword as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @model_year <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-built_in\">SMALLINT<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To declare multiple variables, you separate variables by commas:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @model_year <span class=\"hljs-built_in\">SMALLINT<\/span>, \n        @product_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='assigning-a-value-to-a-variable'>Assigning a value to a variable <a href=\"#assigning-a-value-to-a-variable\" class=\"anchor\" id=\"assigning-a-value-to-a-variable\" title=\"Anchor for Assigning a value to a variable\">#<\/a><\/h2>\n\n\n\n<p>To assign a value to a variable, you use the <code>SET<\/code> statement. For example, the following statement assigns <code>2018<\/code> to the <code>@model_year<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> @model_year = <span class=\"hljs-number\">2018<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-variables-in-a-query'>Using variables in a query <a href=\"#using-variables-in-a-query\" class=\"anchor\" id=\"using-variables-in-a-query\" title=\"Anchor for Using variables in a query\">#<\/a><\/h2>\n\n\n\n<p>The following <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code> statement uses the&nbsp;<code>@model_year<\/code> variable in the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code> clause to find the products of a specific model year:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    product_name,\n    model_year,\n    list_price \n<span class=\"hljs-keyword\">FROM<\/span> \n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span> \n    model_year = @model_year\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    product_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @model_year <span class=\"hljs-built_in\">SMALLINT<\/span>;\n\n<span class=\"hljs-keyword\">SET<\/span> @model_year = <span class=\"hljs-number\">2018<\/span>;\n\n<span class=\"hljs-keyword\">SELECT<\/span>\n    product_name,\n    model_year,\n    list_price \n<span class=\"hljs-keyword\">FROM<\/span> \n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span> \n    model_year = @model_year\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    product_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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 loading=\"lazy\" decoding=\"async\" width=\"485\" height=\"347\" src=\"https:\/\/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\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png 485w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block-300x215.png 300w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/figure>\n\n\n\n<p>The following picture shows the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"208\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-output.png\" alt=\"Stored Procedure Variables - output\" class=\"wp-image-804\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-output.png 379w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-output-300x165.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='storing-query-result-in-a-variable'>Storing query result in a variable <a href=\"#storing-query-result-in-a-variable\" class=\"anchor\" id=\"storing-query-result-in-a-variable\" title=\"Anchor for Storing query result in a variable\">#<\/a><\/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 <code>@product_count<\/code> with the integer data type:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @product_count <span class=\"hljs-built_in\">INT<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, use the <code>SET<\/code> statement to assign the query&#8217;s result set to the variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> @product_count = (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        <span class=\"hljs-keyword\">COUNT<\/span>(*) \n    <span class=\"hljs-keyword\">FROM<\/span> \n        production.products \n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, output the content of the <code>@product_count<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> @product_count;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Or you can use the <code>PRINT<\/code> statement to print out the content of a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">PRINT @product_count;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>or<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">PRINT 'The number of products is ' + CAST(@product_count AS VARCHAR(MAX));\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output in the messages tab is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">The number of products is 204\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To hide the number of rows affected messages, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> NOCOUNT <span class=\"hljs-keyword\">ON<\/span>;    \n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='selecting-a-record-into-variables'>Selecting a record into variables <a href=\"#selecting-a-record-into-variables\" class=\"anchor\" id=\"selecting-a-record-into-variables\" title=\"Anchor for Selecting a record into variables\">#<\/a><\/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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> \n    @product_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>),\n    @list_price <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, assign the column names to the corresponding variables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    @product_name = product_name,\n    @list_price = list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span>\n    product_id = <span class=\"hljs-number\">100<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, output the content of the variables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    @product_name <span class=\"hljs-keyword\">AS<\/span> product_name, \n    @list_price <span class=\"hljs-keyword\">AS<\/span> list_price;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"41\" src=\"https:\/\/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 class=\"wp-block-heading\" id='accumulating-values-into-a-variable'>Accumulating values into a variable <a href=\"#accumulating-values-into-a-variable\" class=\"anchor\" id=\"accumulating-values-into-a-variable\" title=\"Anchor for Accumulating values into a variable\">#<\/a><\/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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span>  PROC uspGetProductList(\n    @model_year <span class=\"hljs-built_in\">SMALLINT<\/span>\n) <span class=\"hljs-keyword\">AS<\/span> \n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> @product_list <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>);\n\n    <span class=\"hljs-keyword\">SET<\/span> @product_list = <span class=\"hljs-string\">''<\/span>;\n\n    <span class=\"hljs-keyword\">SELECT<\/span>\n        @product_list = @product_list + product_name \n                        + <span class=\"hljs-built_in\">CHAR<\/span>(<span class=\"hljs-number\">10<\/span>)\n    <span class=\"hljs-keyword\">FROM<\/span> \n        production.products\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        model_year = @model_year\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n        product_name;\n\n    PRINT @product_list;\n<span class=\"hljs-keyword\">END<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this stored procedure:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, we declared a variable named <code>@product_list<\/code> 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 <code>@model_year<\/code>. In the select list, we accumulated the product names to the <code>@product_list<\/code> variable. Note that the <code>CHAR(10)<\/code> returns the line feed character.<\/li><li>Third, we used the <code>PRINT<\/code> statement to print out the product list.<\/li><\/ul>\n\n\n\n<p>The following statement executes the <code>uspGetProductList<\/code> stored procedure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">EXEC uspGetProductList 2018<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"428\" height=\"235\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-Stored-Procedure-Example.png\" alt=\"Stored Procedure Variables - Stored Procedure Example\" class=\"wp-image-806\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-Stored-Procedure-Example.png 428w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-Stored-Procedure-Example-300x165.png 300w\" sizes=\"auto, (max-width: 428px) 100vw, 428px\" \/><\/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<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"802\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\"\n\t\t\t\tdata-post-title=\"Variables\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"802\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\"\n\t\t\t\tdata-post-title=\"Variables\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":777,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-802","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Variables in SQL Server Stored Procedures<\/title>\n<meta name=\"description\" content=\"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Variables in SQL Server Stored Procedures\" \/>\n<meta property=\"og:description\" content=\"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T13:13:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/\",\"name\":\"Variables in SQL Server Stored Procedures\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/Stored-Procedure-Variables-execute-a-code-block.png\",\"datePublished\":\"2018-09-30T10:49:23+00:00\",\"dateModified\":\"2020-04-11T13:13:13+00:00\",\"description\":\"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/Stored-Procedure-Variables-execute-a-code-block.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/Stored-Procedure-Variables-execute-a-code-block.png\",\"width\":485,\"height\":347,\"caption\":\"Stored Procedure Variables - execute a code block\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Stored Procedures\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Variables in SQL Server Stored Procedures","description":"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/","og_locale":"en_US","og_type":"article","og_title":"Variables in SQL Server Stored Procedures","og_description":"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-04-11T13:13:13+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/","name":"Variables in SQL Server Stored Procedures","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png","datePublished":"2018-09-30T10:49:23+00:00","dateModified":"2020-04-11T13:13:13+00:00","description":"This tutorial introduces you to variables including declaring variables, setting their values, and assigning value fields of a record to variables.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/Stored-Procedure-Variables-execute-a-code-block.png","width":485,"height":347,"caption":"Stored Procedure Variables - execute a code block"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Stored Procedures","item":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/"},{"@type":"ListItem","position":3,"name":"Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/802\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/777"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}