我有一个 UDF 函数,我发现了一些获得更好性能的技巧:
我都试过了,但我无法让“WITH SCHEMABINDING”工作。错误:“无法在架构绑定对象或约束表达式中指定 sql CLR 类型”
这是为什么?
我的代码
ALTER FUNCTION [SIF_get_SalesAmountDosMain1]
(
-- Add the parameters for the function here
@DossierCode T_Code_Dossier
)
RETURNS T_Fin_Amount
--WITH RETURNS NULL ON NULL INPUT
WITH SCHEMABINDING
AS
BEGIN
DECLARE @DetailCode T_Code_Detail
, @DetailSubCode T_Code_DetailSub
, @TotalSalesAmount T_Fin_Amount
, @SalesAmount T_Fin_Amount
Select @TotalSalesAmount = 0
DECLARE
Cur_DosMain CURSOR LOCAL FORWARD_ONLY STATIC FOR
SELECT DetailCode
, DetailSubCode
FROM dbo.T_DossierDetail
Where DossierCode = @DossierCode
FOR READ ONLY
OPEN Cur_DosMain
FETCH Cur_DosMain INTO @DetailCode, @DetailSubCode
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT dd.DetailCode,dd.DetailSubCode
FROM dbo.T_DossierDetail dd
Inner Join dbo.T_Part p on p.PartCode = dd.PartCode
Where dd.DossierCode = @DossierCode
Select @TotalSalesAmount = @TotalSalesAmount + IsNull(@SalesAmount, 0)
FETCH Cur_DosMain INTO @DetailCode, @DetailSubCode
End
CLOSE Cur_DosMain
DEALLOCATE Cur_DosMain
-- Return the result of the function
RETURN @TotalSalesAmount
END
GO