SQLServer 表值函数与标量值函数 定义方式与调用区别

SQLServer 表值函数与标量值函数 定义方式与调用区别

转载自:http://blog.sina.com.cn/s/blog_648861b901012ay2.html

SQLServer 表值函数与标量值函数 定义方式与调用区别

       写sql存储过程经常需要调用一些函数来使处理过程更加合理,也可以使函数复用性更强,不过在写sql函数的时候可能会发现,有些函数是在表值函数下写的有些是在标量值下写的,区别是表值函数只能返回一个表,标量值函数可以返回基类型。

标量值函数创建:

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


表值函数创建:

Create Function [dbo].[GetAllGoods]

()

Returns Table

As

 Return(Select * From [Master_Goods])


创建一个自定义样式的标量函数:

Create Function [dbo].[GetMyStyleDate](@Date DateTime)

Returns nvarchar(20)

Begin

       Declare @ReturnValue nvarchar(20)

       Set @ReturnValue = '今天是' + convert(nvarchar(4),datepart(year,@Date)) + '年'+ convert(nvarchar(2),datepart(month,@Date)) + '月'+ convert(nvarchar(2),datepart(day,@Date)) + '日'

       return @ReturnValue

End

猜你喜欢

转载自blog.csdn.net/dongliang_shali/article/details/50757605