Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 74435 Viewers
80 Video lessons · 129686 Viewers
52 Video lessons · 63918 Viewers
59 Video lessons · 49712 Viewers
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.
Your file was successfully uploaded.