Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Next, I'd like to talk about returning data from a stored procedure using a cursor. A cursor is a data structure that contains multiple rows of data, and it allows us to cycle through each row one at a time and perform an action on each row. Returning a cursor from a stored procedure is pretty similar to returning data from a stored procedure using output parameters. And in a lot of ways you can think of a cursor as a special type of output parameter. I have some code on the screen. This code is available in your exercise files.
The top-half of this code is going to create a stored procedure called procedure cursor that accepts one parameter that's called Authors, and that parameter has a data type that is defined by two words; one word is CURSOR and the second word is VARYING. Meaning, this cursor isn't always going to be exactly the same. It could be a different size based on the data that's currently in the database. This cursor will sometimes return a different number of records than other times. And then the last word is OUTPUT. That's the same as we've had before because this cursor will be used as an output parameter.
We still need the keyword OUTPUT. In the body of the stored procedure on line 4, we set the author's cursor equal to a cursor, and then we put a SELECT statement. So line 6, 7, and 8, I wrote a SELECT statement that's just going to select the first name from the author's table. Just keeping it simple right now. Line 10 is very important where we open the author's cursor. Notice in this stored procedure we never close the author's cursor. We will close it; we're just going to close it outside of the stored procedure.
So in the stored procedure, we only open it. On line 13, we have the keyword GO, and I'm going to scroll down so we can see what happens after this. This is all the code to execute the stored procedure, and get back that cursor. So the top two lines are declaring variables, then line 18 executes the stored procedure, and takes the output and puts it into my cursor. Line 20 and 21, cycle through the first record in my cursor, and then very important, 23 and 24 close the cursor, and de-allocate the cursor.
Close the cursor means it cannot be used anymore, de-allocate means it is removed from memory. It's important to do these things because the machine will not do them for you automatically. If you do not specifically say de- allocate the cursor, it will remain in memory for quite a long time; probably will remain there until you reboot your SQL server. So for the sake of efficiency, you always want to de-allocate. Let's go ahead and run this, and we see the stored procedure is created and it returns one record; the first name of the first author in the table.
We could if we wanted to modify this code at the bottom to do another fetch to get the next record, and another fetch to get the next record, so on, and so forth. Or most likely, you would put it in a loop to walk through all of the items in the cursor. Some developers are very much against cursors. Cursors can take up a lot of memory space, obviously the amount of memory they use is always proportional to the size of the table. So if you're working with a small amount of data, cursors are a good idea.
If you're working with a very large table, you might want to look at an alternative. As mentioned earlier, there are particular performance problems if you do not remove the cursor from memory. So again, the DEALLOCATE statement is important and the DEALLOCATE statement has to come after the CLOSE statement. So when working with cursors, remember, keep it small and always destroy what you create.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98559 Viewers
61 Video lessons · 85845 Viewers
71 Video lessons · 69745 Viewers
56 Video lessons · 102050 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.