SQL Server Programming Part 1 – Stored Procedure Basics

welcome to this wise our tutorial in this session we're going to teach you the basics of working with stall procedures in Microsoft sequel server what you're covering this short session is all about how to work with simple store procedures we'll begin with an explanation of what a stored procedure actually is and then we'll go through the process of creating a basic procedure using a simple select statement as the basis we'll show you a couple of ways of executing your store procedures how you can modify and make changes to a procedure later on and finally how you can delete a procedure that you no longer need so let's get started a stored procedure is simply a group of SQL statements grouped together under a single heading they're similar to methods or subroutines in other programming languages you might have encountered they've got several benefits but for me the main ones are their speed and efficiency so just a quick example of that I've got a basic query here that's been written to show me a list of films and some basic aggregate statistics about their running time in minutes when I'm group by clause and order by clause it's fairly simple query we execute it it'll spit out a set of results so there's nothing too complicated there the downside to this is if I need to run this query many times on a regular basis I haven't need to write out this set of code again and again and again or I need to save the file and then reopen it and run it each time I need to get the results with a stored procedure however if I've created one as I have earlier I can execute that stored procedure or that set of code in a much more simple and quick easy fashion execute this page here I get exactly the same set of results but without having to either open another file or write out all that code again so in this tutorial we're going to show you the absolute basics of how you can get to this stage and a simple stored procedure that you can execute for our first example we're going to use this basic select statement to create our first stall procedure and you can do this with literally any select statement that you've written doesn't matter how complicated it is to use it to create a stored procedure all I need to do is write a create procedure statement above the Select statement you can actually also get away with just writing quick proc which is what I usually do is I'm a bit lazy like that after the word proc or procedure you'd only think of a sensible name for your procedure I always star mine with letters sp hopefully for obvious reasons and this is going to be a basic film list so SP film list the only thing you have to do then is write the word as I usually do this on a separate line and then that will convert the select statement below into a stall procedure when we execute this code before I do execute this page to create my stored procedure is worth mentioning a couple of other things after the as keyword it's often useful to write a begin and end block to contain all of the statements that belong to your store procedure it's not actually necessary in this case because we only have one simple select statement put in more complex procedures where you're writing many many statements the beginning the M block groups all of the useful statements together in one block another useful thing we can do as well is right to use statement above the create statement to make sure that you're going to create the procedure in the appropriate database so you may have seen new statements before you can write them above above simple select clean lines as well so I'm going to say to use movies now one problem with the use statement and the create procedure statement is that the create procedure statement must be the first statement in a batch and at this point use movies is currently the first statement in the batch the way to solve that little problem is to write a go statement which begins a new batch of statements that's a quick comment there begins a new batch so there we go although many of these things aren't actually necessary you don't have to write the use statement you don't have to write the beginning the end block they're useful things to write in it's worthwhile getting into that habit now that we've written out all the code all the remains is to execute this script to create the store procedure not only that in the standard ways I would normally execute a query and the thing you're looking for over the obviously his commands completed successfully down in the messages window at the bottom notice they don't actually see the results of this like statement itself and this this message refers to the fact that the procedure has been created properly so let's just check that that's actually happened and to find your store procedure you can look in the object Explorer and open it in place this for the moment and you'll need to look into your movies and database all the database in which you've created the procedure and looked for the programmability folder in there should be a folder called store procedures I'm going to see there's a few in here already and in fact there's the one that I've just created one called SP film list it's worth mentioning that this list doesn't always refresh immediately so sometimes when you come to look at the store procedures list the one you've just created isn't actually displayed in that case what you can do is select the database in which you've created the stored procedure and then manually refresh it using the refresh button that's of the object Explorer and you'll then have to re-expand the programmability folder and the store procedures folder that's usually enough to force your stored procedure to appear once you've created the procedure you've got several choices for how you can execute it if you still got the page open which you use to create the procedure in the first place in really straightforward way with such a basic procedure you can simply highlight the name of it and then execute that and in the bottom in the results pane you'll see the results of the stored procedure now hopefully obviously in the long run you won't have this page still available once you create the procedure you simply usually discard this so normally what you would do is have a new query window in which you type in the word exact or execute if you prefer and then the name of your stored procedure SP film list now annoyingly if you're using the intellisense you may well see that the name of the stored procedure is underlined in red similarly because the the intellisense list hasn't get refreshed if it annoys you then you can head to the Edit menu choose intellisense and then choose the Refresh local cache or just press ctrl + Shift + R on your keyboard and for a brief pause that usually clears out the problem it would have worked anyway and I executed this and but sometimes it annoys annoys people when they see the red squiggly in the lines anyway now that you've written out this simple statements you can execute this page and again that will execute all of the code is stored in the store procedure so now that you've seen how to create and execute a basic stored procedure what happens if you'd like to modify it and change the way that it works a couple of simple ways that you can do that if you still have this the scripts open which you used to create the procedure in the first place you can simply head back to this one and let's see well let's make a really simple change let's change the the order by Clause so the source our films in descending order rather than ascending now I can't just get away with trying to create this procedure again if it already exists if I try to execute this procedure again now you will tell me that there is already an object called SP filmless in the database fortunately that's a really easy change to make to make sure this will work all I need to do is make sure that the the word create is changed to the word alter instead if I exclude this procedure now that will modify and updates the way my store procedure works and if I finally head back to the page that I had created earlier on and we execute this I should find that instead of ascending order my films now appear in descending order instead I suppose the next obvious question is what if you want to modify a stored procedure and you don't still have the page which is created in the first place well just to show you that there's nothing to worry about so I'm going to close down this page and I'm not going to take the changes but it's absolutely fine I don't need that page in the first place to modify the procedure once it exists and I can access it through my object Explorer I want to make any changes to it I can simply right-click and choose to modify it and that will open up a new query window with all the code that I use to create the procedure in the first place with a word alter and a couple of other bits and pieces as well we don't really need in fact all these extra bits and pieces are completely irrelevant to what we're doing and I'm going to delete those so let's see if I wanted to modify this again let's change it back to an ascending order and maybe we could include an extra field as well I see we've got film run time in minutes that's half the film Oscar wins as well nominations I've gone for it anyway that will do if I exclude this page again that will update my store procedure I can then close down this window and I don't need to save any changes and then if I execute my store procedure one more time I'll see that it includes my new field the final thing to quickly show you in this tutorial is how you can remove a store procedure that you no longer need two main ways that you can do this if you're working in management studio the simplest thing to do is to right-click on the name of the procedure you want to remove and simply choose to delete it you'll be asked to confirm that with a dialog box I was just going to click cancel and this at this example you can also choose to remove a store procedure and by writing a statement for that you can write the drop product core procedure espy film list which is the name of the one you want to remove and then simply execute that if I do so I'll be showing that my command is completed successfully again slightly confusingly the the list in the object Explorer doesn't refresh immediately what you can do is either select the programmability folder or the movies database and then refresh the page manually and then when you react on the programmability folder and store procedures we should find that the film list procedure has been removed so there you go there's all the basics of how you can create execute modify and delete symbol store procedures if you've enjoyed this training video you can find many more online training resources at ww-why 0 UK

Tags: , , , ,


Leave a Reply

Your email address will not be published. Required fields are marked *