Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
This course investigates several key database-programming concepts: triggers, stored procedures, functions, and .NET CLR (Common Language Runtime) assemblies. Author Martin Guidry shows how to combine these techniques and create a high-quality database using Microsoft SQL Server 2012. The course also covers real-world uses of the INSERT, UPDATE, and DELETE procedures, and how to build a basic web form to connect to your database.
Now I'd like to talk about returning data through stored procedures, specifically returning data as datasets primarily. We'll also talk a little bit about a return value. I have some code on the screen here that is available in our exercise files. This is very similar to what we were doing in the previous section. It's code that allows us to modify a stored procedure and execute that stored procedure. I'm going to modify the stored procedure to have a return value. So at the end, the very last thing before the end statement, I will simply issue the keyword Return, and give it a numeral.
So in this case, Return 1. When we execute that, we will likely not see that one returned just yet, we have to go through a little more effort to actually see what is returned. We will declare a variable to hold that value. We will then execute the stored procedure, and take what is returned, and store it in our newly created variable, and then we'll need one last line of code to display that returned value.
Go ahead and execute all of that, and so we see we got two results in the Results pane at the bottom; one says, Hello World, this is fun which is the data returned from the stored procedure, and then also the numeral that we returned, and in this case, we returned 1. We could change that easily to return a different numeral and now it returns 12. Microsoft recommends as a best practice every stored procedure should return a value. Typically you return either 0 or 1 for success or failure.
So if the stored procedure failed for some reason, you would return a 0, if it was successful, you would return 1. Different people like to have a little different naming conventions for that. For example, if the stored procedure returned an error, you could return a code that signifies an error in your mind, maybe something like -1. But the bottom line is Microsoft recommends, and I also agree with this recommendation, the last line of every stored procedure should be returned a numeral to me, and that numeral should have some meaning.
Beyond returning numerals, you probably want your stored procedure to return some data. And we've been doing that; we have Select, and then we're returning some text. We can return multiple datasets by having multiple SELECT statements. So I'll have a second SELECT statement that will return a second set of data. Now at the bottom, we see that we return three different things; the first data set, below that the second dataset, and then finally the return value.
You could additionally add a third dataset, a fourth dataset, a fifth dataset. The machine does not put an upper limit on what we can and cannot return. Good common sense says, you probably want to limit what one stored procedure returns, but two or three datasets is not uncommon. Notice I said that the return -1 or return whatever numeral you have should be the last line of the stored procedure. Let's see what happens if you violate that recommendation, and we put that in the middle right here.
You notice it returned the first dataset, "Hello World this is fun." It returned the -1, it did not return the phrase "This is more text." The machine stops returning after the line with the keyword RETURN. It will perform the return on that line. It will not do any returns after that. So the keyword RETURN almost always needs to be the very last thing in your stored procedure. So in this demonstration, we've seen returning data from a stored procedure as either a numeral or a dataset.
In our next video, we'll talk about returning data as a cursor.
There are currently no FAQs about SQL Server: Triggers, Stored Procedures, and Functions.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.