{"id":140,"date":"2019-09-24T07:50:38","date_gmt":"2019-09-24T07:50:38","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=140"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"parameters","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/parameters\/","title":{"rendered":"Parameters"},"content":{"rendered":"\n<h2>Creating a stored procedure with one parameter<\/h2>\n\n\n\n<p>The following query returns a product list from the&nbsp;<code>products<\/code>&nbsp;table in the&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>SELECT&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;list_priceFROM &nbsp;&nbsp;&nbsp;&nbsp;production.productsORDER BY&nbsp;&nbsp;&nbsp;&nbsp;list_price;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>You can create a stored procedure that wraps this query using the&nbsp;<code>CREATE PROCEDURE<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011<\/td><td>CREATE PROCEDURE uspFindProductsASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>However, this time we can add a parameter to the stored procedure to find the products whose list prices are greater than an input price:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213<\/td><td>ALTER PROCEDURE uspFindProducts(@min_list_price AS DECIMAL)ASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&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;list_price &gt;= @min_list_price&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul><li>First, we added a parameter named&nbsp;<code>@min_list_price<\/code>&nbsp;to the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure. Every parameter must start with the&nbsp;<code>@<\/code>&nbsp;sign. The&nbsp;<code>AS DECIMAL<\/code>&nbsp;keywords specify the data type of the&nbsp;<code>@min_list_price<\/code>&nbsp;parameter. The parameter must be surrounded by the opening and closing brackets.<\/li><li>Second, we used&nbsp;<code>@min_list_price<\/code>&nbsp;parameter in the&nbsp;<code>WHERE<\/code>&nbsp;clause of the&nbsp;<code>SELECT<\/code>&nbsp;statement to filter only the products whose list prices are greater than or equal to the&nbsp;<code>@min_list_price<\/code>.<\/li><\/ul>\n\n\n\n<h3>Executing a stored procedure with one parameter<\/h3>\n\n\n\n<p>To execute the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure, you pass an argument to it as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXEC uspFindProducts 100;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-One-parameter-example.png\" alt=\"SQL Server Stored Procedure Parameters - One parameter example\" class=\"wp-image-791\"\/><\/figure>\n\n\n\n<p>The stored procedure returns all products whose list prices are greater than or equal to 100.<\/p>\n\n\n\n<p>If you change the argument to 200, you will get a different result set:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXEC uspFindProducts 200;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-one-parameter-change-argument-example.png\" alt=\"SQL Server Stored Procedure Parameters - one parameter change argument example\" class=\"wp-image-790\"\/><\/figure>\n\n\n\n<h2>Creating a stored procedure with multiple parameters<\/h2>\n\n\n\n<p>Stored procedures can take one or more parameters. The parameters are separated by commas.<\/p>\n\n\n\n<p>The following statement modifies the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure by adding one more parameter named&nbsp;<code>@max_list_price<\/code>&nbsp;to it:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011121314151617<\/td><td>ALTER PROCEDURE uspFindProducts(&nbsp;&nbsp;&nbsp;&nbsp;@min_list_price AS DECIMAL&nbsp;&nbsp;&nbsp;&nbsp;,@max_list_price AS DECIMAL)ASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&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;list_price &gt;= @min_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price &lt;= @max_list_price&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Once the stored procedure is modified successfully, you can execute it by passing two arguments, one for&nbsp;<code>@min_list_price<\/code>&nbsp;and the other for&nbsp;<code>@max_list_price<\/code>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXECUTE uspFindProducts 900, 1000;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following shows the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-multiple-parameters-example.png\" alt=\"SQL Server Stored Procedure Parameters - multiple parameters example\" class=\"wp-image-789\"\/><\/figure>\n\n\n\n<h2>Using named parameters<\/h2>\n\n\n\n<p>In case stored procedures have multiple parameters, it is better and more clear to execute the stored procedures using named parameters.<\/p>\n\n\n\n<p>For example, the following statement executes the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure using the named parameters&nbsp;<code>@min_list_price<\/code>and&nbsp;<code>@max_list_price<\/code>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>EXECUTE uspFindProducts &nbsp;&nbsp;&nbsp;&nbsp;@min_list_price = 900, &nbsp;&nbsp;&nbsp;&nbsp;@max_list_price = 1000;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The result of the stored procedure is the same however the statement is more obvious.<\/p>\n\n\n\n<h2>Creating text parameters<\/h2>\n\n\n\n<p>The following statement adds the&nbsp;<code>@name<\/code>&nbsp;parameter as a character string parameter to the stored procedure.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819<\/td><td>ALTER PROCEDURE uspFindProducts(&nbsp;&nbsp;&nbsp;&nbsp;@min_list_price AS DECIMAL&nbsp;&nbsp;&nbsp;&nbsp;,@max_list_price AS DECIMAL&nbsp;&nbsp;&nbsp;&nbsp;,@name AS VARCHAR(max))ASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&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;list_price &gt;= @min_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price &lt;= @max_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name LIKE &#8216;%&#8217; + @name + &#8216;%&#8217;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code>&nbsp;clause of the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>&nbsp;statement, we added the following condition:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>product_name LIKE &#8216;%&#8217; + @name + &#8216;%&#8217;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>By doing this, the stored procedure returns the products whose list prices are in the range of min and max list prices and the product names also contain a piece of text that you pass in.<\/p>\n\n\n\n<p>Once the stored procedure is altered successfully, you can execute it as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>EXECUTE uspFindProducts &nbsp;&nbsp;&nbsp;&nbsp;@min_list_price = 900, &nbsp;&nbsp;&nbsp;&nbsp;@max_list_price = 1000,&nbsp;&nbsp;&nbsp;&nbsp;@name = &#8216;Trek&#8217;;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this statement, we used the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure to find the product whose list prices are in the range of 900 and 1,000 and their names contain the word&nbsp;<code>Trek<\/code>.<\/p>\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\/SQL-Server-Stored-Procedure-Parameters-text-parameter-example.png\" alt=\"SQL Server Stored Procedure Parameters - text parameter example\" class=\"wp-image-793\"\/><\/figure>\n\n\n\n<h2>Creating optional parameters<\/h2>\n\n\n\n<p>When you execute the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure, you must pass all three arguments corresponding to the three parameters.<\/p>\n\n\n\n<p>SQL Server allows you to specify default values for parameters so that when you call stored procedures, you can skip the parameters with default values.<\/p>\n\n\n\n<p>See the following stored procedure:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819<\/td><td>ALTER PROCEDURE uspFindProducts(&nbsp;&nbsp;&nbsp;&nbsp;@min_list_price AS DECIMAL = 0&nbsp;&nbsp;&nbsp;&nbsp;,@max_list_price AS DECIMAL = 999999&nbsp;&nbsp;&nbsp;&nbsp;,@name AS VARCHAR(max))ASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&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;list_price &gt;= @min_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price &lt;= @max_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name LIKE &#8216;%&#8217; + @name + &#8216;%&#8217;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this stored procedure, we assigned&nbsp;<code>0<\/code>&nbsp;as the default value for the&nbsp;<code>@min_list_price<\/code>&nbsp;parameter and&nbsp;<code>999,999<\/code>&nbsp;as the default value for the&nbsp;<code>@max_list_price<\/code>&nbsp;parameter.<\/p>\n\n\n\n<p>Once the stored procedure is compiled, you can execute it without passing the arguments to&nbsp;<code>@min_list_price<\/code>&nbsp;and&nbsp;<code>@max_list_price<\/code>&nbsp;parameters:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>EXECUTE uspFindProducts &nbsp;&nbsp;&nbsp;&nbsp;@name = &#8216;Trek&#8217;;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-Optional-Parameters.png\" alt=\"SQL Server Stored Procedure Parameters - Optional Parameters\" class=\"wp-image-792\"\/><\/figure>\n\n\n\n<p>In this case, the stored procedure used&nbsp;<code>0<\/code>&nbsp;for&nbsp;<code>@min_list_price<\/code>&nbsp;parameter and&nbsp;<code>999,999<\/code>&nbsp;for the&nbsp;<code>@max_list_price<\/code>&nbsp;parameter when it executed the query.<\/p>\n\n\n\n<p>The&nbsp;<code>@min_list_price<\/code>&nbsp;and&nbsp;<code>@max_list_price<\/code>&nbsp;parameters are called optional parameters.<\/p>\n\n\n\n<p>Of course, you can also pass the arguments to the optional parameters. For example, the following statement returns all products whose list prices are greater or equal to&nbsp;<code>6,000<\/code>&nbsp;and the names contain the word&nbsp;<code>Trek<\/code>:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>EXECUTE uspFindProducts &nbsp;&nbsp;&nbsp;&nbsp;@min_list_price = 6000,&nbsp;&nbsp;&nbsp;&nbsp;@name = &#8216;Trek&#8217;;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-Pass-Optional-Parameters.png\" alt=\"SQL Server Stored Procedure Parameters - Pass Optional Parameters\" class=\"wp-image-795\"\/><\/figure>\n\n\n\n<h2>Using NULL as the default value<\/h2>\n\n\n\n<p>In the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure, we used&nbsp;<code>999,999<\/code>&nbsp;as the default maximum list price. This is not robust because in the future you may have products with the list prices that are greater than that.<\/p>\n\n\n\n<p>A typical technique to avoid this is to use&nbsp;<code>NULL<\/code>&nbsp;as the default value for the parameters:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12345678910111213141516171819<\/td><td>ALTER PROCEDURE uspFindProducts(&nbsp;&nbsp;&nbsp;&nbsp;@min_list_price AS DECIMAL = 0&nbsp;&nbsp;&nbsp;&nbsp;,@max_list_price AS DECIMAL = NULL&nbsp;&nbsp;&nbsp;&nbsp;,@name AS VARCHAR(max))ASBEGIN&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&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;list_price &gt;= @min_list_price AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@max_list_price IS NULL OR list_price &lt;= @max_list_price) AND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name LIKE &#8216;%&#8217; + @name + &#8216;%&#8217;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In the&nbsp;<code>WHERE<\/code>&nbsp;clause, we changed the condition to handle&nbsp;<code>NULL<\/code>&nbsp;value for the&nbsp;<code>@max_list_price<\/code>&nbsp;parameter:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>(@max_list_price IS NULL OR list_price &lt;= @max_list_price) <\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following statement executes the&nbsp;<code>uspFindProducts<\/code>&nbsp;stored procedure to find the product whose list prices are greater or equal to 500 and names contain the word&nbsp;<code>Haro<\/code>.<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>EXECUTE uspFindProducts &nbsp;&nbsp;&nbsp;&nbsp;@min_list_price = 500,&nbsp;&nbsp;&nbsp;&nbsp;@name = &#8216;Haro&#8217;;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Parameters-NULL-as-default-values.png\" alt=\"SQL Server Stored Procedure Parameters - NULL as default values\" class=\"wp-image-796\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Creating a stored procedure with one parameter The following query returns a product list from the&nbsp;products&nbsp;table in the&nbsp;sample database: 1234567 SELECT&nbsp;&nbsp;&nbsp;&nbsp;product_name,&nbsp;&nbsp;&nbsp;&nbsp;list_priceFROM &nbsp;&nbsp;&nbsp;&nbsp;production.productsORDER BY&nbsp;&nbsp;&nbsp;&nbsp;list_price; You can create a stored procedure that wraps this query using the&nbsp;CREATE&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\/140"}],"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=140"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":141,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions\/141"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}