I'm leaving... who initiated the title so rude? Change it... huh, take care of him! The teacher told me not to care about these details. let's start our topic. Here we will discuss the table value functions in SQL. this article may not be of much significance to fellow citizens who are familiar with him, but it is useful for children's shoes who are just getting started. when I first came into contact
I'm leaving... who initiated the title so rude? Change it... huh, take care of him! The teacher told me not to care about these details. let's start our topic. Here we will discuss the table value functions in SQL. this article may not be of much significance to fellow citizens who are familiar with him, but it is useful for children's shoes who are just getting started. when I first came into contact
I'm leaving... who initiated the title so rude? Change it... huh, take care of him! The teacher told me not to care about these details.
Let's start our topic. Here we will discuss the table value functions in SQL.
This article may not be of much significance to fellow citizens who are familiar with him, but it is useful for children's shoes who are just getting started.
When I first came into contact, I had a lot of effort (maybe I am stupid! Here is a simple and concise example to discuss this seemingly magical thing.
A table-valued function is a user-defined function.
"Table value" indicates that the returned value is a table.
Table value functions are divided into inline Table value functions and multi-statement Table value functions (in fact, a certain angle is the same, don't be confused ).
Let's first look at the inline Table value function. Next we will open our SQL statement editing tool, and we will write the first inline Table value function.
We are not panic. Since it is called a function, it must have the features of a function. Let's guess... such as brackets, parameters...
Yes! Its syntax is not difficult:
Create function Name (parameter, parameter...) RETURNS TABLE
AS
RETURN
(
Statement
)
GO
First, create function indicates creating a FUNCTION. The parameter list is in brackets.
Returns table indicates that the returned value is a TABLE.
The RETURN (statement) after AS is the part of function execution.
The difference between an inline table valued function and a multi-statement table valued function is actually the number of internal SQL query statements, and the writing method is slightly different.
Let's do the following:
Create function myfun (@ NUMBER1 INT, @ NUMBER2 INT)
RETURNS TABLE
AS
RETURN (select @ NUMBER1 + @ NUMBER2 as result)
-- This function is very easy to do, that is, adding the incoming two values (of course, this example is not very good, because if you know that "Scalar Value Function" is not very differentiated, it is easy to confuse, but here it is just for simplicity)
A function is declared. The parameter is of the INT type @ NUBMER1 and @ NUMBER2, and the returned value is TABLE.
Then return the RESULT of adding two values. Because the returned table column is in the "anonymous" format, the column must be an alias. This is called "RESULT ".
Call the function:
SELECT * from myfun (2, 3)
-- Of course, 5 is returned.
Are you and your friends stunned by this function call method? Joke .. But is it unexpected?
Don't worry, think about it. Since it is a table value function, the key is to return "tables", ao! No, it's a "table". Should we look up the table in SQL when querying (calling a function is equivalent to querying the returned table?
By the way, isn't that okay? Yeah! I'm in Greater China. (It seems that there is no such thing as a dime)
We were no longer interested after getting in touch with her. Well, let's brainstorm and see what this is...
(It seems like this guy looks amazing... what did she do? Will it be... you want to do it yourself !)
Create function myfun (@ NUMBER1 INT, @ NUMBER2 INT)
RETURNS @ tab table (result nvarchar (32 ))
AS
BEGIN
Insert into @ tab select @ NUMBER1 + @ NUMBER2
UPDATE @ tab set result = RESULT * 10
RETURN
END
GO
-- After a hard-pressed brainstorm, We are depressed. The good thing is that you have guessed it. It is a multi-statement Table value function.
You may say, ah, there are too many statements here, but you haven't seen any clues between it and the inline Table value function...
As mentioned above, the number of internal SQL statements is different from the number of inline table valued functions and multi-statement table valued functions.
The above example shows that one of my statements is to fill the @ TAB table, and the other is to update the value of its RESULT column, which is in the RETURN (...) of the inline Table value function (...) parentheses are not allowed because only one statement is allowed in the brackets.
If there is more than one, an error will be reported.
It is worth mentioning that in a multi-statement Table value function, the "RETURN Statement", that is, the RETURN flag, must exist, because it does not indicate the RETURN (statement) as early as the value function of the inline table, it needs to be manually returned.
The second note is that in the Multi-statement Table value function, the declared RETURN table must have data (even if it is null) before the RETURN ), in other words, it must be initialized; otherwise, an error will be reported "The ** statement contained in the function cannot return data to the client ".
Now, we suddenly find that table value functions are not difficult, right?
The example in this example is relatively simple. Of course, you can use it to do big things as much as you can! Haha
Okay! Late at night, we have uncovered its veil, and it has been exposed to you. You and your friends are stunned...
Joke! Goodbye!
> Tip: the code is pure and has no errors. If you copy the code to the SQL editor and report an error, please try to delete the indentations or replace them again (because the tabs in the blog editor seem to be different in the SQL editor)
,