Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A stored procedure is defined with the create procedure statement. For this lesson I'm going to use the album database. And we're going to start by creating a simple procedure. Now, a procedure may have multiple lines in it. And these multiple lines may be delimited with semicolons. And in order for that entire block of. Text. That entire block of code to be sent to the server all at once. We need to actually change the delimiter for a short time, temporarily, to something else, so that that entire block can be sent to the server as a unit.
So that looks like this. I use the DELIMITER command. And I changed delimiter to something other than a semicolon like that. I'll change it back when we're done and then, I type CREATE PROCEDURE and I name the procedure and I have BEGIN and END and, after the end, I put in that double-slash and then, this entire block along with the statements. I'm just going to put in a SELECT statement here for now. That entire block from create procedure all the way down to the end all gets sent to the server as one unit, and that's important.
If it were delimited with the semicolon, the client in this case, SID, would send that partial command to the server and it would be a syntax error and it wouldn't work. And so now I set my delimiter back. To a semicolon. And I can say CALL list albums, like that. And when I press Go, my procedure is defined, and my procedure is tested and it works. And so now all of this, can be deleted and CALL list albums will give us that exact same result.
So the delimiter is a MySQL specific thing. In fact it's implemented in the client and so I had to implement it in SID myself in PHP. But it works exactly the same way in the MySQL command line client, and this is just an example of a very simple procedure, so that we can test this process. Now let's do something a little bit more substantial I'm going to come out here to the exercise file this is the chapter nine. Exercise file and here under O3 stored procedures I'm going to grab this second one here.
I'm going to start with this DROP PROCEDURE IF EXISTS and, I'm going to come all the way down here to CALL list album. So I'm going to grab all of that, copy and paste it, and let's just take a look at it here in the text editor you can see this is. Just a more substantial SELECT statement. It's a JOIN statement and you'll notice that it also uses this track len function that we defined in the last lesson. So, that's a stored function that's being called from a stored procedure. You will also notice that this procedure has a parameter it's called a and it's of type VARCHAR and that is used here in the where clause so it's WHERE artist LIKE a and so we are going to pass it some thing when we call that we are going to pass it a string.
That can be used in that like clause. So I'll go ahead and I'll press Go, and we get this, all the tracks where the artist is like hendrix. Right there the way that we called this. So again it's fairly simple, you can do much more complex stuff. In a stored procedure. But I want to just show you one more example with an output parameter. And that is this next one here. Here we have a procedure with an input parameter, and they default to input.
I could have typed IN like that, but it's just not necessary because it defaults to that. And an output parameter called d, so in is a and out is d. And here I have a simple select. I'm selecting all the tracks and I'm actually summing the duration of the tracks. And I'm putting that result INTO the d variable. So I say, SELECT track length, the sum of duration INTO d. And that puts it in the d variable. And it's where album id IN.
And I have a subselect with the artist LIKE a. And so we call it with a set to the hendrix with the wildcards and a global variable with an @ sign, and a global variable with an sign and that global variable will be filled with that result from d. So the procedure stores the output value in this dur variable. By storing it in d inside of the procedure. And so, I'm just going to grab all of this from the DROP PROCEDURE IF EXISTS all the way down to the SELECT and paste this in and press Go.
And we get that result. And again, all of this is stored now on the server. So, I can just CALL duration with these parameters. Instead of hendrix, I could say Winter, for Johnny Winter, and I'll get a different result. And that's calling this function that's actually stored in the server. Stored functions and procedures are deleted with the drop function and drop procedures. So I can say DROP FUNCTION, track len.
And DROP PROCEDURE. And I've got two of them. Total duration and list albums. And I call that. And that drops all the functions and procedures that we've defined in this lesson. Leaving the data in the database intact. So stored procedures are used in a statement context whereas stored functions are used in an expression context. This is an important distinction. I suggest you experiment a lot with both functions and procedures to get a good feel for how and where you will use them in your own code.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98592 Viewers
61 Video lessons · 85891 Viewers
71 Video lessons · 69770 Viewers
56 Video lessons · 102077 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.