我有函数的语法,但是对于如何将条件放入代码(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