T-SQL language provides us with a more flexible way to manipulate data, that is, functions, functions are divided into three main categories: scalar function: (pass in a parameter, and then a parameter) aggregate function (pass in multiple parameters, outgoing one parameter), table-valued function (passing in a result set object, So that we can manipulate the table by manipulating it, the table-valued functions are subdivided into two categories: inline table-valued functions and multi-statement table-valued functions. The inline table-valued function looks like this:
1 CREATE FUNCTION my_fun ()RETURNSTABLE2as3 RETURN 4 SELECT * from Table_1
The error-prone detail of writing functions is the difference between returns and return
The above is the inline table-valued function, and then look at the multi-statement table-valued Function:
The difference between an inline table-valued function and a multi-statement table-valued function is indicated in the textbook:
The inline table-valued function does not have a function subject (Begin-end) that returns the result set of a single select, whereas a multi-statement table-valued function is composed of multiple T-SQL statement sequences between Begin-end, which can generate record rows and insert rows into a table, and then return a table. ----The third edition of SQL Server tutorial
The following is an example of a multi-statement table-valued Function:
CREATE FUNCTION myfun_2 ()RETURNS@my_list( char), Char(ten) )as BEGIN INSERT @my_list-- operation END
The function can add some modifiers to the value when it is passed, including read-only, default, and so on, similar to other languages.
Summary of the understanding of functions in SQL Server