我有一个不是由我编写的存储过程。
你能帮我翻译一下CASE
条款中的WHERE
陈述吗?
--Declaring the parameter for SP
DECLARE
@CompanyGuids varchar(8000) = '29634AF7-D0A2-473D-9574-405C23E10F02'
--Declaring table variable that will contain only CompanyGuid
DECLARE @CompanyGuidsTbl TABLE(Guid uniqueidentifier)
IF @CompanyGuids IS NULL
BEGIN
INSERT INTO @CompanyGuidsTbl
SELECT DISTINCT CompanyGuid FROM tblCompanies
END
ELSE
BEGIN
INSERT INTO @CompanyGuidsTbl
SELECT * FROM dbo.StringOfGuidsToTable(@CompanyGuids,',')
END
--Select statement
SELECT Col1,
Col2,
Col3
FROM MyTable1 INNER JOIN MyTable2
/* this is where I am confused */
WHERE
CASE WHEN @CompanyGuids IS NOT NULL
THEN
CASE WHEN tblCompanies.CompanyGuid in (SELECT Guid FROM @CompanyGuidsTbl)
THEN 1 ELSE 0 END
ELSE 1
END = 1
如果我错了,请纠正我:
"So if the parameter @CompanyGuids is NOT NULL, then we are checking if table @CompanyGuidsTbl has assigned parameter and if it does - then we gonna use it, but if it does not - ??? "
也许有办法重写它? 感谢
答案 0 :(得分:2)
一个构造不良的声明肯定,但是它是最终检查真值声明,其中1 = 1.首先检查一个空变量,然后如果变量不为空它检查tblCompanies中的CompanyGUID是否在提供的列表中在变量中,如果找到则返回1(因此1 = 1为真,因此记录匹配),或者如果未找到(在这种情况下0 = 1,这是假的,因此记录不匹配)。可怕的东西!