在表值函数中声明变量

时间:2018-07-16 18:10:56

标签: sql-server tsql

下面是我的表格函数。 我正在尝试将模拟参数传递给我的函数参数

ALTER FUNCTION [dbo].[GetContractBudgetDetail] ( @ContractId INT )
RETURNS TABLE
AS
RETURN
    (   
         SELECT     
                CBS.SdId AS Id,
                CBS.ServiceName,
                CBS.ServiceType,
                CBS.BudgetAmount AS OverallServiceBudgetAmount,
                BS.TotalPriorServicePayments AS OverallPaymentAmount,
                SUM(ISNULL(cbs.BudgetAmount,0) - ISNULL(BS.TotalPriorServicePayments,0)) as ServiceAvailable,
                ISNULL(CAST(CASE WHEN CRT.Approved ='Y' THEN 1 ELSE 0 END AS bit ),0) AS IsContractApproved, --1
                ISNULL(CAST(CASE WHEN PC.SD_id IS NOT NULL THEN 1 ELSE 0 END AS bit ),0) AS IsProviderVisible,
                ISNULL(CAST(CASE WHEN BP.BudgetParentId IS NOT NULL THEN 1 ELSE 0 END AS bit),0) AS IsBudgetVisible,
                @ContractId AS contractId --2: as per model             

            INNER JOIN Contract CRT ON CRT.Id = @ContractId--Not necessary to inclue this for all but need bcz of model--1
            LEFT JOIN GetPaymentsforBudgetedServicesbyContractId(@ContractId) BS ON CBS.SdId = BS.BudgetServiceDetail_Id
            LEFT JOIN (
                        SELECT  DISTINCT JRPS.SD_id
                        FROM    JuncContractResourceContractors JCRC
                                LEFT JOIN JuncResourceProviderServiceDetail JRPS ON JCRC.Id = JRPS.JCRC_Id
                        WHERE   JCRC.Contract_Id=@ContractId
                      ) PC ON CBS.SdId = PC.SD_id
            LEFT JOIN   (
                        SELECT  DISTINCT FSD.BudgetParentId
                        FROM    JuncContractServiceDetail JCSD
                                INNER JOIN fServiceDetail FSD ON FSD.Id = JCSD.SD_Id
                        WHERE   JCSD.Contract_Id = @ContractId
                        ) BP ON BP.BudgetParentId = CBS.SdId
    GROUP BY    CBS.SdId, CBS.BudgetAmount, 
                BS.BudgetServiceDetail_Id, BS.TotalPriorServicePayments, 
                CBS.ServiceName, CBS.ServiceType,
                CRT.Approved, --1,
                PC.SD_id,
                BP.BudgetParentId
    );

我想声明一个参数并传递给这样的函数参数

declare @conId int;
set @conId = @ContractId;

有人可以建议如何做吗?

0 个答案:

没有答案