SQL stored procedures often need to call some functions to make the processing process more reasonable and make the function more reusable. However, you may find that when writing SQL functions, some functions are written under the table value function and some are written under the scalar value. The difference is that the table value function can only return one table, and the scalar value function can return the base type.
Scalar Value 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 value function creation:
Create Function [DBO]. [getallgoods]
()
Returns table
As
Return (select * from [master_goods])
Create a scalar function for 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) + 'Year' + convert (nvarchar (2), datepart (month, @ Date) + 'month' + convert (nvarchar (2), datepart (day, @ Date) + 'day'
Return @ ReturnValue
End
The method for calling the value-winning function is as follows: Select dbo. GoosWidth ('20140901 ')
Note: The owner dbo must be added to the front of the function.
You can call a table-valued function by using the following method: Select * From GetAllGoods.