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
       其中純量值函式調用的時候方式如下:Select dbo.GoosWidth('0003')
       注意:函數前邊一定要加上所有者:dbo 
       資料表值函式調用方法如下:Select * From GetAllGoods() 資料表值函式調用的時候不用加入。