SQL Server table-valued function and scalar-valued function define the way and call difference
SQL stored procedures often need to call some functions to make the process more reasonable, you can also make functions more reusable, but when writing SQL functions you may find that some functions are written under a table-valued function and some are written under a scalar value, and the difference is that a table-valued function can return only one table, and a scalar-valued function may return a base type.
Scalar-valued Function creation: Create function [dbo]. [Gooswidth]
(
@goodscode varchar (20)
)
Returns float
Begin
DECLARE @value float
Select @value = Goodswidth from master_goods where Goodscode = @goodscode
Return (@value)
End
Table-valued Function creation: Create function [dbo]. [Getallgoods]
()
Returns table
As
Return (SELECT * from [Master_goods])
Creates a scalar function of a custom style: Create function [dbo]. [Getmystyledate] (@date datetime)
Returns nvarchar (20)
Begin
declare @returnvalue nvarchar (20)
Set @returnvalue = ' Today is ' + convert (nvarchar (4), DATEPART (year, @date)) + ' years ' + convert (nvarchar (2), DATEPART (month, @date)) + ' month ' + convert (nvarchar (2), DATEPART (day, @date)) + ' th '
Return @returnvalue
End
The scalar-valued function is called when the method is as follows: Select Dbo.gooswidth (' 0003 ') Note: The function must precede the owner: dbo
Table-valued function invocation methods are as follows: SELECT * from Getallgoods () table-valued function call without adding/*