gpt4 book ai didi

sql - 简化 If 和 else IF 查询

转载 作者:行者123 更新时间:2023-12-01 09:49:23 25 4
gpt4 key购买 nike

ALTER PROCEDURE spDisplayTableNames1
@T_ID INT OUT,
@BatchNumber VARCHAR(30) OUT
AS
BEGIN
IF((SELECT COUNT(*) FROM tblPacks
WHERE T_ID = @T_ID
AND BatchNumber = @BatchNumber) = 0)
AND ((SELECT COUNT(*) FROM tblBlisters
WHERE T_ID = @T_ID
AND BatchNumber = @BatchNumber) = 0)
AND ((SELECT COUNT(*) FROM tblShippers
WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber) = 0)
BEGIN
PRINT '0 0 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '1 0 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '0 1 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '0 0 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
BEGIN
Print '1 1 0'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '1 0 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '0 1 1'
END
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)>0)
BEGIN
Print '1 1 1'
END

END

这里我正在检查三个表的条件,如果一个列存在于三个表中,它应该打印“1 1 1”,如果它只存在于第一个和第三个表中,则打印“1 0 1”。就这样我写了 8 个条件语句。但是我想要一个简化查询上面的存储过程

我想要上面示例的最简单查询 - 我该怎么做?

最佳答案

试试这个:

declare @prt varchar(10);
if ((SELECT COUNT(*) FROM tblPacks WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
set @prt = '0';
else
set @prt = '1';

if ((SELECT COUNT(*) FROM tblBlisters WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
set @prt = @prt + ' 0';
else
set @prt = @prt + ' 1';

if ((SELECT COUNT(*) FROM tblShippers WHERE T_ID=@T_ID AND BatchNumber=@BatchNumber)=0)
set @prt = @prt + ' 0';
else
set @prt = @prt + ' 1';

print @prt;

关于sql - 简化 If 和 else IF 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com