SQL Server table-valued function and scalar-valued function define the way and call difference

Source: Internet
Author: User
Tags one table scalar

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/*

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.