Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
It's very common that as you start inserting information into your databases, what you're going to need or at least what your applications are going to need is to find out a bit more information about what you just inserted. Particularly in the case where you are inserting information into a table that is going to generate keys for you, either identity columns like this one, ProductCategoryID just going up one by one, or guids that are being built as well or perhaps both at the same time.
But in the INSERT example we saw before, all that I'm inserting is a ParentProductCategoryID and a name, because the guids and the identities are being generated for us. Now, we want to get that information after this INSERT has happened. This used to be a bit of a problematic thing to do. In fact often what you used to have to do is write little pieces of code to generate that guid yourself and then insert it yourself, so that you knew what it was. Well, it's a lot simpler these days. What we just do is we take our INSERT statement and we change it just a little bit.
See, before the VALUES that we were inserting, I'm using this new clause called OUTPUT. And we can actually use this on an INSERT, we can use it on an UPDATE, we can use it on a DELETE. OUTPUT is a special kind of a unique clause here. What it allows us access to is the inserted columns that are just about to happen and it knows they haven't happened yet, but it knows that we will have them. In fact, if I say OUTPUT inserted, notice that inserted is in IntelliSense, and I hit that, I hit the dot, and that's even smart enough to tell me here in IntelliSense, "Well, you are inserting into ProductCategories, so these are your only options.
Which one would you like?" Well! Let's say that what I want to do is I want to find the ProductCategoryID that's going to be generated as an identity field. And now when I hit Execute, what happens is I don't just get the phrase "one row affected," I get the new ProductCategoryID that I just created, which was ProductCategoryID number 56. Now you can even output multiple inserted values. So I can hit comma. I want now the inserted guid that's happening there.
I have got to be careful because I'm running another INSERT statement and Commuter Bikes already inserted there and that has to be unique. So I'll just change that to Custom Bikes, hit Execute, and we get back 57, which was to be expected. And the new generated rowguid. That again is being set because there's a default constraint on the ProductCategory table that will generate a new rowguid every time. Now in essence, what's going to happen here is if you've got an application that is executing this code, it's almost like having a mini SELECT statement that returns these values afterwards.
It's just a lot easier to write. And we can see later how this might be added to and inserted into say a stored procedure to make it a bit more friendly and a bit more reusable. But this is the general idea of using OUTPUT clause. In fact, I will show you another example. If I wanted to do a DELETE, I'm going to say that I'm going to DELETE FROM, same table not surprisingly. Again usually I just put the WHERE clause. Always want a WHERE clause when I'm doing this DELETE.
So ProductCategoryID = well let's say the one that we just entered in a couple minutes ago, which was 56. And again, I'm putting in the OUTPUT clause. Well! It's not going to be inserted this time around. That doesn't make sense but I will have an OUTPUT deleted. and I can say I'd like to know what the Name was. I'd like to know what the deleted guid was. We hit Execute and it tells us the name was Commuter Bikes and that was the rowguid.
So we can get that information back as well. I try and execute that again, not surprisingly I get no output because there is no row with ProductCategoryID equal to 56 anymore. Now it can also be used on an UPDATE statement but it's probably not what you are expecting. Here's the deal. When we construct a regular UPDATE statement here such as UPDATE SalesLT.ProductCategory, of course we have to use our SET, what are we actually doing here. I am going to just do a SET parent ProductCategoryID = 1, WHERE ProductCategoryID = 57.
Now, usually you might be ahead of me, thinking well, we put the OUTPUT in here and we had an inserted. for insert and a deleted. for deleted. So presumably we have an updated. And no, we don't. Here's the deal. What that inserted word and the deleted word represent is essentially a temporary table in memory. If we do an INSERT, the inserted table represents what happened after the INSERT.
If we do a DELETE the deleted table represents what got deleted and if we do an UPDATE, we don't have an updated one. Wat we really have is the inserted table. It's the changes that were made. So if I want to say output some information about the roq that's just getting updated, I could say OUTPUT inserted.Name. So hit that Execute and we find that we have just updated ProductCategoryID number 57 with a ParentID of 1 where the name was Custom Bikes and we are able to get that information out about what we just updated.
Nine times out of ten, the real use for the OUTPUT clause is going to be after an INSERT. That's the most important thing for most application developers is allow them to insert a new row and find out immediately what the identity or the guid of that row was, but it can also be used with your UPDATE and with your DELETE.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 75401 Viewers
80 Video lessons · 130160 Viewers
52 Video lessons · 64266 Viewers
59 Video lessons · 50081 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.