0

我有函数的语法,但是对于如何将条件放入代码(SQL)中,我完全没用,所以它会返回一个答案。

我有三张桌子,

T1.number varchar(max)
T1.date datetime
T1.ctps int

T2.number varchar(max)
T2.addedDate date
T2.RemovedDate date
T2.ctps int

T3.OType varchar(max)
T3.LNumber varchar(max)

和三个变量,

@LNumber varchar(max), 
@OType varchar(max), 
@CDate datetime

我需要将行号带入表 1 的功能检查数字是否在其中,如果是,则需要获取 OType 名称,如果是 LTD 或 PLC,则需要检查 CPTS 标志是否设置为 1如果是,则需要根据 T1.Date 检查合同日期 如果合同日期在 T1.Date 之前,则返回“否”,否则如果合同日期晚于 T1.Date,则返回“是”

这是我的尝试,不是很好,一位朋友建议在我的函数中使用 if else 语句,但不知道如何实现,哈哈

任何帮助,将不胜感激

IF OBJECT_ID(N'TPS_Checker', N'FN') IS NOT NULL 

    DROP FUNCTION TPS_Checker ;

GO


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>    --@LineNumber = u.CLI, @OrganisationType = o.Name, @ContractDate = c.ContractDate 
-- =============================================
CREATE FUNCTION TPS_Checker
(@LineNumber VARCHAR(MAX), @ContractDate DATETIME, @OrganisationType VARCHAR)
RETURNS VARCHAR(4)
AS


BEGIN

DECLARE @Answer varchar(4)

SELECT @Answer = CASE
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType  = 'PLC') OR @OrganisationType  = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType  = 'PLC') OR @OrganisationType  = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 1) THEN 'No' 
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 1) THEN 'No'
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType  = 'PLC') OR @OrganisationType  = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 1) THEN 'Yes'
                WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 0) THEN 'Yes'
                END


FROM [Contract] c
INNER JOIN [Account] a
        ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
        ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
        ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
        ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN    (      SELECT BranchTypeFK, BranchID, Name   
                            FROM [Branch] b
                        )b
        ON [d].[BranchFK] = [b].[BranchID]
        AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
        ON [p].[OrganisationTypeFK]  = [o].[OrganisationTypeID] 
LEFT JOIN  [Lookup] U
        ON U.CLI  = l.LineNumber
LEFT JOIN History H
        ON h.CLI  = l.LineNumber

return @answer

    -- Return the result of the function


RETURN @Answer
END

    select top 100 [CORPORATE\HargreavesD].[TPS_Checker1](u.CLI ,c.ContractDate,o.Name) Answer 
FROM [Contract] c
INNER JOIN [Account] a
        ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
        ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
        ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
        ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN    (      SELECT BranchTypeFK, BranchID, Name   
                            FROM [Branch] b
                        )b
        ON [d].[BranchFK] = [b].[BranchID]
        AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
        ON [p].[OrganisationTypeFK]  = [o].[OrganisationTypeID] 
LEFT JOIN  [Lookup] U
        ON U.CLI  = l.LineNumber
LEFT JOIN History H
        ON h.CLI  = l.LineNumber

这是我的第一次尝试,但不是功能

DECLARE @StartDate DATE = '01/04/2013'
DECLARE @EndDate DATE = '30/04/2013'

SELECT  ContractID
        ,LineNumber
        ,ContractDate
        ,[Date]
        ,o.Name
        ,u.[File]
        ,DateAdded
        ,DateRemoved
        ,u.CTPS
        ,B.Name [Branch Name]

        END
INTO [TPS]
FROM [Contract] c
INNER JOIN [Account] a
    ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
    ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
    ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
    ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN  (   SELECT BranchTypeFK, BranchID, Name   
                FROM [Branch] b
            )b
    ON [d].[BranchFK] = [b].[BranchID]
    AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
    ON [p].[OrganisationTypeFK]  = [o].[OrganisationTypeID] 
LEFT JOIN   (
            SELECT [CLI], [Date] [lDate], [Date], [CTPS], [File] = 'Lookup'  
            FROM [Lookup] tl
            )u
    ON u.CLI  = l.LineNumber
LEFT JOIN   (
            SELECT [CLI], [DateAdded], [DateRemoved], [CTPS], [File] = 'History'  
            FROM History h
            )h
    ON h.CLI  = l.LineNumber 
WHERE ContractDate BETWEEN @StartDate AND @EndDate

SELECT  *
        ,[OnTPS] =  CASE
                    WHEN ([Date] IS NOT NULL AND [ContractDate] < [Date]) THEN 'No'
                    WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date]) THEN 'Yes' 

                    WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved]) THEN 'Yes'
                    WHEN ([Date] IS NULL AND [ContractDate] NOT BETWEEN [DateAdded] AND [DateRemoved]) THEN 'No'

                    WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
                    WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'

                    WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
                    WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'
                    END
FROM [Temp].[dbo].[TPS] t
4

1 回答 1

0

您的主要查询可以稍微简化,但仍然给出相同的结果(我认为 - 没有测试数据很难判断):

    SELECT @Answer = CASE
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 1 THEN 'No' 
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 1 THEN 'No'
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate > [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate > [U].[Date] AND [U].[CTPS] = 1 THEN 'Yes'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate > [U].[Date] AND [U].[CTPS] = 0 THEN 'Yes'
        END
    FROM [Contract] c
        INNER JOIN [Account] a ON [c].[AccountFK] = [a].[AccountID]
            INNER JOIN [Profile] p ON [p].[ProfileID] = [a].[ProfileFK]
                INNER JOIN [OrganisationType] o ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID] 
        INNER JOIN [Line] l ON [l].[ContractFK] = [c].[ContractID]
            INNER JOIN [Lookup] U ON U.CLI  = l.LineNumber
        LEFT JOIN [Deal] d ON [c].[ContractID] = [d].[ContractFK]
            INNER JOIN  [Branch] b ON [d].[BranchFK] = [b].[BranchID] AND BranchTypeFK IN (1,2,4)
    WHERE [U].[CLI] = @LineNumber
      AND [U].[Date] IS NOT NULL

您还在@Answer函数底部返回了两次,并且在某些情况下@Answer可能仍然是NULL; 这可能是故意的。除此之外,如果它满足您的业务需求并以可接受的速度执行,则没有真正的驱动程序可以进一步重写此功能。

于 2013-07-11T09:53:35.717 回答