{"id":8821,"date":"2022-11-14T06:57:00","date_gmt":"2022-11-14T05:57:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=8821"},"modified":"2022-07-17T13:55:27","modified_gmt":"2022-07-17T11:55:27","slug":"return-values-sql-server-stored-procedure","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/return-values-sql-server-stored-procedure\/","title":{"rendered":"Return values with SQL Server stored procedure"},"content":{"rendered":"\n<p>How to create a SQL Server stored procedure to return values with OUTPUT? This simple T-SQL example returns values through output parameters. Setting up, instantiating, and using the output parameters of a stored procedure is similar to that of the input parameters.<\/p>\n\n\n\n<p>The major difference is the OUTPUT clause after the parameter name. The OUTPUT keyword specifies that the stored procedure must return a value. On the other hand, the output clause can be specified using either the keyword &#8220;OUTPUT&#8221; or simply &#8220;OUT&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-return-values-into-variables-with-a-sql-server-stored-procedure-using-the-output-keyword\">Return values into variables with a SQL Server stored procedure using the output keyword<\/h2>\n\n\n\n<p>How to retrieve and store the result of a stored procedure after an execute command in a SQL Server variable? Indeed, during the call of the commands execute SQL or EXEC, it is easy to display the result with the PRINT function.<\/p>\n\n\n\n<p>It is also possible and useful to retrieve it in a variable to use it in the following T-SQL code. <\/p>\n\n\n\n<p>First of all, create the clients table, the example code for the creation of the table is available here :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Create the clients table to store the number, the name and the city\nCREATE TABLE [dbo].[CLIENTS] (\n   [ClientNumber] \tint IDENTITY(1,1),\n   [Name]       \tnvarchar(20) UNIQUE,\n   [City]     \t\tnvarchar(20)\n)\nGO\n<\/pre>\n\n\n\n<p>Example of a T-SQL procedure to return data with the OUTPUT option<br>In fact, this example procedure returns the name of the city of a customer whose name is passed as an input parameter.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Creatie a stored procedure with the variable @CityCustomer as OUTPUT\nCREATE PROCEDURE dbo.uspClientCity\n\t@ClientName nvarchar(20),\n\t@CustomerCity nvarchar(20) OUTPUT\nAS\n\tSELECT \t@ClientCity= CITY\n\tFROM \tdbo.CLIENTS\n\tWHERE \t[Name] = @ClientName\nGO\n<\/pre>\n\n\n\n<p>Here is an example of a script to create a SQL Server stored procedure with OUTPUT to retrieve and reuse the result in one or more variables after the execution of an Exec command.<\/p>\n\n\n\n<p>Indeed, in T-SQL with the EXEC command, followed by the name of the procedure, use the keyword &#8220;OUTPUT&#8221;. This allows to store the result from the called procedure and to send it to the caller.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Execute the SQL Server stored procedure with OUTPUT in 3 steps<\/h3>\n\n\n\n<p>First, declare a variable of the same type as the OUTPUT return and the City column, i.e. nvarchar(20) for our example.<br>Second, execute the procedure with the EXEC command.<br>Select the variable @CityCustomer that contains the result of the stored procedure.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Declare the variable to store the city\nDECLARE @ClientCity nvarchar(20)\n\n-- Execute the stored procedure\nEXEC dbo.uspClientCity\n\t@ClientName = 'SERGEI',\n\t@CustomerCity = @CustomerCity OUTPUT\n\n-- Select the client city from the variable\nSELECT @ClientCity as [City];\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT returns the result of a T-SQL command in a variable<\/h2>\n\n\n\n<p>Finally, the result of the stored procedure is the value of the city for the selected client.<\/p>\n\n\n\n<p>To go further on stored procedures, here is how to insert rows in a stored procedure. This example passes the values as parameters in <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/language-elements\/variables-transact-sql?view=sql-server-ver16\" target=\"_blank\" rel=\"noreferrer noopener\">variables<\/a>.<\/p>\n\n\n\n<p>For more scripting examples, here is <strong><a href=\"https:\/\/expert-only.com\/en\/t-sql\/alter-sql-server-stored-procedure\/\">how to modify a SQL Server stored procedure<\/a><\/strong> with ALTER PROCEDURE.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-tutoriels-et-exemples-sql-server-et-microsoft-it wp-block-embed-tutoriels-et-exemples-sql-server-et-microsoft-it\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"MeDT4LHWpC\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/alter-sql-server-stored-procedure\/\">How To Alter A SQL Server Stored Procedure ?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How To Alter A SQL Server Stored Procedure ?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/alter-sql-server-stored-procedure\/embed\/#?secret=bFonaQxVJQ#?secret=MeDT4LHWpC\" data-secret=\"MeDT4LHWpC\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to create a SQL Server stored procedure to return values with OUTPUT? This simple T-SQL example returns values through output parameters. Setting up, instantiating, and using the output parameters of a stored procedure is similar to that of <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/return-values-sql-server-stored-procedure\/\" title=\"Return values with SQL Server stored procedure\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":5811,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-8821","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=8821"}],"version-history":[{"count":1,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8821\/revisions"}],"predecessor-version":[{"id":29031,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/8821\/revisions\/29031"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/5811"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=8821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=8821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=8821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}