Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Business and personalized recommendations.Start Your Free Trial Now
- View Offline
- Comparing triggers, functions, and stored procedures
- Installing and configuring SQL Server
- Creating a stored procedure
- Returning data using data sets
- Creating user-defined functions
- Using "after," "instead," and nested triggers
- Modifying existing stored procedures
- Implementing logging on DELETE
- Choosing between T-SQL and CLR
- Executing a stored procedure
- Passing parameters
Skill Level Advanced
Now I'd like to talk a little bit about functions, sometimes called user-defined functions in SQL Server 2012. I'm going to start off with creating a new function. So very similar to creating a stored procedure. We start off with the keyword CREATE and then the keyword FUNCTION. And then we need to give a name, typical naming rules in play here. After the name, you have to supply a list of parameters in parenthesis. So we could list out as many parameters as we want.
You also have the option to do zero parameters which if you'd like to do zero parameters, you still have to put the empty parenthesis, which I will do here. Next, we have to declare what type of data, this function is going to return. Every function will return something and we don't at this point have to decide the exact value we're returning, but we do have to decide what data type you're going to return. So are you going to return a date, a piece of text, a number? You have to make that decision at this time.
You type in the keyword RETURNS, plural, and I'll use an integer for this one. So this function will return an integer. Then we put the keyword AS and the keyword BEGIN. Most of these things do not have to be on separate lines. I just find it makes it a little bit easier to read, if you do in fact put them all on separate lines. After the keyword BEGIN, we'll need to add some T-SQL statements that accomplish whatever it is that you want to accomplish. For now, we'll keep it very simple and we're just going to return a value.
I previously declared we would be returning an integer. So I will stick with that and say return 7. Now that I'm done with my function, I will put in the keyword END. I can execute this and we see some positive feedback at the bottom, command(s) completed successfully. I'll go over to the left and expand my favorite menu, Programmability. Underneath that, we see Functions and this is a Scalar-valued Function.
So I'll hit refresh and now my function appears there, my Test Function. So notice under the functions category, we have Table-valued Functions, Scalar-valued Functions, Aggregate Functions and System Functions. The System Functions are the one created by the machine. We cannot change those. The other three Table-valued Functions, Scalar-valued Functions and Aggregate Functions, we will be working within this course starting off with Scalar-valued Functions for now.
So I've successfully created a Scalar- valued Function called myTestFunction, I would like to run that function. Typically, the easiest way to do that is to put it as part of a SELECT statement. So I will say SELECT and then I need to give it the name of the schema, the name of the function, and empty parentheses. When I run that, it returns 7 and that is exactly what we had hoped for returning a single value. Notice, if I don't put the parenthesis, I will in fact receive an error or if I don't list the name of the schema, I will receive an error.
That's a little unusual because at most places in Microsoft SQL Server, the name of the schema is optional, here it is not optional. You do have to in fact have say schema.function name (). That is the only way it's going to work. Back looking at our function, if we would like to modify it, we can alter our function thusly. I wanted to talk briefly about a few common mistakes that happens; the most common of which is the data type mismatch.
So I said I was going to return an integer. What if I write code that doesn't return an integer? Surprisingly, it let me do this! The machine said command(s) completed successfully. But, now let's go back and try and run that function. The error message says; conversion failed when converting from text to number. So it couldn't take a value that I gave it. I told it to return the value test. It could not convert that to an integer and it did give me an error. The surprising part is it allowed me to create the function with the error.
It just never allows me to run the function. So you get the error at a time may be a little different than what you thought. The other thing to be real careful about, when creating these is, the keyword RETURN with no S versus the keyword RETURNS with an S. The first time we use returns, it does have an S, the second time it does not and if you get those backwards. It will say incorrect syntax near the keyword RETURN.
So this first one needs an S and the second one does not.