<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlservernation.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How Tos : return, beginner</title><link>http://sqlservernation.com/blogs/howtos/archive/tags/return/beginner/default.aspx</link><description>Tags: return, beginner</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Stored Procedures</title><link>http://sqlservernation.com/blogs/howtos/archive/2009/03/06/stored-procedures.aspx</link><pubDate>Fri, 06 Mar 2009 23:54:00 GMT</pubDate><guid isPermaLink="false">a3dbd76b-19b9-441f-b40a-d79e8f433d35:73</guid><dc:creator>BrandonGalderisi</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlservernation.com/blogs/howtos/rsscomments.aspx?PostID=73</wfw:commentRss><comments>http://sqlservernation.com/blogs/howtos/archive/2009/03/06/stored-procedures.aspx#comments</comments><description>&lt;p&gt;Stored procedures are part of the core functionality of a DBMS.&amp;nbsp; SQL Server is no exception to that.&amp;nbsp; Stored procedures have many uses.&amp;nbsp; They can be used to handle basic CRUD operations as well as complex logic.&amp;nbsp; There are several things that you have to consider when it comes to building stored procedures for you database.&amp;nbsp; &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is this something that has to be done in the database?&lt;/li&gt;
&lt;li&gt;Is this something that should be done in the database?&lt;/li&gt;
&lt;li&gt;Does my database server have the capacity to handle this as it&amp;#39;s use scales.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Above there are three questions you need to ask yourself but they are not the only three.&amp;nbsp; Since there is no way for us to answer any of these questions, we&amp;#39;ll talk briefly about it and concentrate on the what you can do and how.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Is this something that has to be done in the database?&lt;br /&gt;&lt;/b&gt;There are benefits to keeping your logic contained in the database.&amp;nbsp; It&amp;#39;s easy to patch by installing new procedures or updating existing ones.&amp;nbsp; All of the logic is contained inside of one location instead of being spread out in embedded SQL in applications.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Is this something that should be done in the database?&lt;br /&gt;&lt;/b&gt;SQL Server&amp;#39;s primary purpose is to store your information.&amp;nbsp; And it&amp;#39;s great at a lot of things, and not at others.&amp;nbsp; The more work that is done in the database, the higher the load placed on the database server.&amp;nbsp; This can lead to a lot of issues including increased CPU and memory usage which can take away from SQL Server&amp;#39;s ability to do what it HAS to do.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Does my database server have the capacity to handle this as it&amp;#39;s use scales?&lt;br /&gt;&lt;/b&gt;I can&amp;#39;t tell you how many times I&amp;#39;ve heard people say &amp;quot;there will only be 10 users&amp;quot; or &amp;quot;there will never be more than a few hundred records&amp;quot;.&amp;nbsp; If you write inefficient SQL because you expect small usage, then by habit you will write inefficient SQL for large scale operations.&amp;nbsp; I&amp;#39;m not saying to over-engineer every table.&amp;nbsp; Sometimes it is OK to bend the rules a little.&amp;nbsp; But it has to be a clear and conscious decision to do such&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now on to &lt;b&gt;Stored Procedures&lt;/b&gt;.&amp;nbsp; I will first start by showing a very simple stored procedure that shows the structure, then we&amp;#39;ll discuss it&amp;#39;s structure.&amp;nbsp; There are are lot of different things to know, even about the basics of stored procedures.&amp;nbsp; So this will be a rather long posting.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectHello&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;select &amp;#39;Hello&amp;#39;&lt;br /&gt;return&lt;br /&gt;go&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Above is a very basic, and impractical, procedure that does nothing other than select the word &amp;#39;Hello&amp;#39;.&amp;nbsp; To create a stored procedure, you start off by saying &amp;quot;CREATE PROCEDURE&amp;quot;.&amp;nbsp; The next thing that you do is give the name.&amp;nbsp; You do not have to specify the schema, but it&amp;#39;s a good idea to do so.&amp;nbsp; Above, I have selected the dbo schema.&amp;nbsp; The next thing after the name is the keyword &amp;quot;as&amp;quot;.&amp;nbsp; As has many uses in SQL Server.&amp;nbsp; In stored procedures, it is used to separate the definition of the stored procedure from the code of the stored procedure.&amp;nbsp; The next line, &amp;quot;set nocount on&amp;quot; is not specific to stored procedures, but it will prevent the stored procedure from outputing the number of rows effected by various operations in the query.&amp;nbsp; Next is the simple select statement followed by return.&amp;nbsp; Return is not required in stored procedures.&amp;nbsp; But when a return is executed, the procedure exits immediately.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;EXEC dbo.up_SelectHello&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Calling the procedure from within SQL Server Management Studio is as simple as running the above command in a query window.&amp;nbsp; An important thing to note is that if you are only executing one command in a batch, you do not need to include the EXEC.&amp;nbsp; So the below statements are all valid was to call the procedure.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;EXEC dbo.up_SelectHello&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;dbo.up_SelectHello&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;dbo.up_SelectHello&lt;br /&gt;go&lt;br /&gt;select &amp;#39;Something Else&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This last is valid because the GO separates the first and second statement into two different batches.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @Input nvarchar(100)&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;select @input&lt;br /&gt;return&lt;br /&gt;go&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
The above goes one step further and uses an input parameter as the text to select.&amp;nbsp; While this procedure is equally useless, it shows the structure of allowing input parameters.&amp;nbsp; The structure is that you include the variable name and it&amp;#39;s data type between the procedure name and the &amp;quot;as&amp;quot; statement.&amp;nbsp; You can also provide multiple parameters by separating them by commas, as shown below.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @Input   nvarchar(100)&lt;br /&gt; ,@Input2  nvarchar(100)&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;select @input + @input2&lt;br /&gt;return&lt;br /&gt;go&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Going to the next level, you can provide default values for input parameters.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @Input nvarchar(100) = N&amp;#39;Hello&amp;#39;&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;select @input&lt;br /&gt;return&lt;br /&gt;go&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What this does is allow the parameter to have a value if the parameter is not specified in the execution call.&amp;nbsp; This does not mean that if you specify NULL that it will use the value&amp;nbsp; So if the following 3 procedure calls are made:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;EXEC dbo.up_SelectInput @input=N&amp;#39;Hello&amp;#39;&lt;br /&gt;EXEC dbo.up_SelectInput &lt;br /&gt;EXEC dbo.up_SelectInput NULL&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The results in order will be:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_hello.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_hello.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_hello.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_hello.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_NULL.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_NULL.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that for the last call, I did not specify @input in the EXEC statement.&amp;nbsp; SQL Server does not require you to specify the parameter name when passing them to a procedure call.&amp;nbsp; Instead, it will accept the parameters in the order in which they are provided.&amp;nbsp; But if you specify the name for a parameter, you must specify the name for all subsequent parameters.&lt;/p&gt;
&lt;p&gt;So with the following procedure:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @Input   nvarchar(100) = N&amp;#39;hello&amp;#39;&lt;br /&gt; ,@Input2  nvarchar(100) = N&amp;#39;goodbye&amp;#39;&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;select @input + N&amp;#39; &amp;#39; + @input2&lt;br /&gt;return&lt;br /&gt;go&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;You will get the following results with these commands:&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;exec dbo.up_SelectInput @input=N&amp;#39;SQL Server&amp;#39; ,@input2=N&amp;#39;Nation&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;exec dbo.up_SelectInput N&amp;#39;SQL Server&amp;#39;,@input2=N&amp;#39;Nation&amp;#39;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;exec dbo.up_SelectInput @input=N&amp;#39;SQL Server&amp;#39;, N&amp;#39;nation&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="color:red;"&gt;Msg 119, Level 15, State 1, Line 1&lt;br /&gt;Must pass parameter number 2 and subsequent parameters as &amp;#39;@name = value&amp;#39;. After the form &amp;#39;@name = value&amp;#39; has been used, all subsequent parameters must be passed in the form &amp;#39;@name = value&amp;#39;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Output parameters allow you to get data OUT of a stored procedure.&amp;nbsp; In order to specify a parameter as an OUTPUT parameter, you add the keyword OUTPUT after the data type definition.&amp;nbsp; This does not mean that you can not pass a value in if it is defined as output, but it allows you to pass a value out.&amp;nbsp; In the below procedure, you will see that we are concatenating the first two parameters and assigning it to the third parameter.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @param1  nvarchar(100) = N&amp;#39;hello&amp;#39;&lt;br /&gt; ,@param2  nvarchar(100) = N&amp;#39;goodbye&amp;#39;&lt;br /&gt; ,@param3  nvarchar(100) output&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;set @param3 = @&lt;/code&gt;&lt;code&gt;param1 &lt;/code&gt;&lt;code&gt;+ N&amp;#39; &amp;#39; + @&lt;/code&gt;&lt;code&gt;param2  &lt;/code&gt;&lt;br /&gt;&lt;code&gt;return&lt;br /&gt;go&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now to get the value from the parameter, it is a little different.&amp;nbsp; Since executing the procedure will not select any data, we have to retrieve the value using a parameter.&amp;nbsp; Below we will pass in the parameter @output to accept the value of @param3.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;declare @output nvarchar(100)&lt;br /&gt;exec dbo.up_SelectInput @param1 = N&amp;#39;SQL Server&amp;#39;, @param2 = &amp;#39;Nation&amp;#39;, @param3 = @output output&lt;br /&gt;select @output&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Selecting @output you will see the the following result.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_SSN.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The last thing that I want to cover in this is the difference between return codes and output parameters.&amp;nbsp; The return code is the status of the procedure.&amp;nbsp; It is the value that is returned by the RETURN statement in the procedure.&amp;nbsp; By default, if you do not specify a value in the RETURN statement, the default of 0 is used.&amp;nbsp; You can only return an INTEGER from a procedure.&amp;nbsp; You can not return dates, or any other data types.&amp;nbsp; Only integers.&amp;nbsp; The default return code of 0 typically indicates that that the procedure executed successfully.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test using return codes.&amp;nbsp; We will start with the same basic procedure that we have been using.&amp;nbsp; and we will show how to call the procedure to capture the return code.&amp;nbsp;
Unlike output parameters, return codes are captured using the &amp;quot;exec
@parameter = procedure&amp;quot; syntax.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @param1  nvarchar(100) = N&amp;#39;hello&amp;#39;&lt;br /&gt; ,@param2  nvarchar(100) = N&amp;#39;goodbye&amp;#39;&lt;br /&gt; ,@param3  nvarchar(100) output&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;set @param3 = @param1 + N&amp;#39; &amp;#39; + @param2  &lt;br /&gt;return &lt;br /&gt;go&lt;br /&gt;&lt;/code&gt;&lt;code&gt;declare @output          nvarchar(100)&lt;br /&gt;       ,@ReturnCode      INT&lt;br /&gt;exec @ReturnCode = dbo.up_SelectInput @param1 = N&amp;#39;SQL Server&amp;#39;, @param2 = &amp;#39;Nation&amp;#39;, @param3 = @output output&lt;br /&gt;&lt;br /&gt;select @ReturnCode&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you will see, the value of @returncode is 0.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_0.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_0.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre style="border:1px dashed #999999;padding:5px;overflow:auto;font-family:Andale Mono,Lucida Console,Monaco,fixed,monospace;color:#000000;background-color:#eeeeee;font-size:12px;line-height:14px;width:100%;"&gt;&lt;code&gt;CREATE PROCEDURE dbo.up_SelectInput&lt;br /&gt;  @param1  nvarchar(100) = N&amp;#39;hello&amp;#39;&lt;br /&gt; ,@param2  nvarchar(100) = N&amp;#39;goodbye&amp;#39;&lt;br /&gt; ,@param3  nvarchar(100) output&lt;br /&gt;as&lt;br /&gt;set nocount on&lt;br /&gt;set @param3 = @param1 + N&amp;#39; &amp;#39; + @param2  &lt;br /&gt;return 1&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;declare @output          nvarchar(100)&lt;br /&gt;       ,@ReturnCode      INT&lt;br /&gt;exec @ReturnCode = dbo.up_SelectInput @param1 = N&amp;#39;SQL Server&amp;#39;, @param2 = &amp;#39;Nation&amp;#39;, @param3 = @output output&lt;br /&gt;&lt;br /&gt;select @ReturnCode&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you will see, the value of @returncode is 1.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://sqlservernation.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_1.PNG"&gt;&lt;img src="http://sqlservernation.com/resized-image.ashx/__size/550x0/__key/CommunityServer.Blogs.Components.WeblogFiles/howtos.Stored+Procedures/res_5F00_1.PNG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Return codes are useful for passing information back about what happened in a procedure.&amp;nbsp; For example a user authentication procedure could be used to validate that a username and password hash passed in are valid for a particular user.&amp;nbsp; Using return codes you could differentiate between a successful authentication, a failed password match, an invalid username, or anything else you would desire.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I hope that you enjoyed this overview on the basic structure of stored procedures.&amp;nbsp; Understanding how to construct stored procedures is an essential skill that you will need if you want to become a SQL Server master.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://sqlservernation.com/aggbug.aspx?PostID=73" width="1" height="1"&gt;</description><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/variable/default.aspx">variable</category><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/stored+procedures/default.aspx">stored procedures</category><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/return/default.aspx">return</category><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/parameter/default.aspx">parameter</category><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/beginner/default.aspx">beginner</category><category domain="http://sqlservernation.com/blogs/howtos/archive/tags/Beginner+SQL/default.aspx">Beginner SQL</category></item></channel></rss>