Difference between definitions and calls of SQL Server table valued functions and scalar functions

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.