{"id":142,"date":"2019-09-24T07:51:36","date_gmt":"2019-09-24T07:51:36","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=142"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"stored-procedure-basics","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/stored-procedure-basics\/","title":{"rendered":"Stored Procedure Basics"},"content":{"rendered":"\n<h2>Creating a simple stored procedure<\/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 returns a list of products from the&nbsp;<code>products<\/code>&nbsp;table in the BikeStores&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  product_name,  list_priceFROM  production.productsORDER BY  product_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>To create a stored procedure that wraps this query, you use the&nbsp;<code>CREATE PROCEDURE<\/code>&nbsp;statement as follows:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011<\/td><td>CREATE PROCEDURE uspProductListASBEGIN&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;product_name;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul><li>The&nbsp;<code>uspProductList<\/code>&nbsp;is the name of the stored procedure.<\/li><li>The&nbsp;<code>AS<\/code>&nbsp;keyword separates the heading and the body of the stored procedure.<\/li><li>If the stored procedure has one statement, the&nbsp;<code>BEGIN<\/code>&nbsp;and&nbsp;<code>END<\/code>&nbsp;keywords surrounding the statement are optional. However, it is a good practice to include them to make the code clear.<\/li><\/ul>\n\n\n\n<p>Note that in addition to the&nbsp;<code>CREATE PROCEDURE<\/code>&nbsp;keywords, you can use the&nbsp;<code>CREATE PROC<\/code>&nbsp;keywords to make the statement shorter.<\/p>\n\n\n\n<p>To compile this stored procedure, you execute it as a normal SQL statement in SQL Server Management Studio 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\/SQL-Server-Stored-Procedure-Compiling.png\" alt=\"\" class=\"wp-image-780\"\/><\/figure>\n\n\n\n<p>If everything is correct, then you will see the following message:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>Commands completed successfully.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>It means that the stored procedure has been successfully compiled and saved into the database catalog.<\/p>\n\n\n\n<p>You can find the stored procedure in the Object Explorer, under&nbsp;<strong>Programmability &gt; Stored Procedures<\/strong>&nbsp;as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-Object-Explorer.png\" alt=\"\" class=\"wp-image-782\"\/><\/figure>\n\n\n\n<p>Sometimes, you need to click the&nbsp;<strong>Refresh<\/strong>&nbsp;button to manually update the database objects in the Object Explorer.<\/p>\n\n\n\n<h2>Executing a stored procedure<\/h2>\n\n\n\n<p>To execute a stored procedure, you use the&nbsp;<code>EXECUTE<\/code>&nbsp;or&nbsp;<code>EXEC<\/code>&nbsp;statement followed by the name of the stored procedure:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXECUTE sp_name;<\/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>EXEC sp_name;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>where&nbsp;<code>sp_name<\/code>&nbsp;is the name of the stored procedure that you want to execute.<\/p>\n\n\n\n<p>For example, to execute the&nbsp;<code>uspProductList<\/code>&nbsp;stored procedure, you use the following statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXEC uspProductList;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The stored procedure returns the following output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-output.png\" alt=\"SQL Server Stored Procedure output\" class=\"wp-image-784\"\/><\/figure>\n\n\n\n<h2>Modifying a stored procedure<\/h2>\n\n\n\n<p>To modify an existing stored procedure, you use the&nbsp;<code>ALTER PROCEDURE<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>First, open the stored procedure to view its contents by right-clicking the stored procedure name and select&nbsp;<strong>Modify<\/strong>&nbsp;menu item:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-modifying.png\" alt=\"SQL Server Stored Procedure modifying\" class=\"wp-image-781\"\/><\/figure>\n\n\n\n<p>Second, change the body of the stored procedure by sorting the products by list prices instead of product names:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567891011<\/td><td> ALTER PROCEDURE uspProductList&nbsp;&nbsp;&nbsp;&nbsp;AS&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product_name, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;production.products&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list_price &nbsp;&nbsp;&nbsp;&nbsp;END;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Third, click the&nbsp;<strong>Execute<\/strong>&nbsp;button, SQL Server modifies the stored procedure and returns the following output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>Commands completed successfully.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Now, if you execute the stored procedure again, you will see the changes taking effect:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>EXEC uspProductList;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The following shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Stored-Procedure-output-changes.png\" alt=\"SQL Server Stored Procedure output changes\" class=\"wp-image-783\"\/><\/figure>\n\n\n\n<h2>Deleting a stored procedure<\/h2>\n\n\n\n<p>To delete a stored procedure, you use the&nbsp;<code>DROP PROCEDURE<\/code>&nbsp;or&nbsp;<code>DROP PROC<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DROP PROCEDURE sp_name;<\/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>DROP PROC sp_name;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>where&nbsp;<code>sp_name<\/code>&nbsp;is the name of the stored procedure that you want to delete.<\/p>\n\n\n\n<p>For example, to remove the&nbsp;<code>uspProductList<\/code>&nbsp;stored procedure, you execute the following statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>DROP PROCEDURE uspProductList;<\/td><\/tr><\/tbody><\/table>\n","protected":false},"excerpt":{"rendered":"<p>Creating a simple stored procedure The following&nbsp;SELECT&nbsp;statement returns a list of products from the&nbsp;products&nbsp;table in the BikeStores&nbsp;sample database: 1234567 SELECT product_name, list_priceFROM production.productsORDER BY product_name; To create a stored procedure that wraps this query, you&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\/142"}],"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=142"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}