2016-12-22 68 views
3
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 [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])=0) 
BEGIN 
Print '1 0 0' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])=0) 
BEGIN 
Print '0 1 0' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])>0) 
BEGIN 
Print '0 0 1' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])=0) 
BEGIN 
Print '1 1 0' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])>0) 
BEGIN 
Print '1 0 1' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])=0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])>0) 
BEGIN 
Print '0 1 1' 
END 
ELSE IF((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])>0) AND ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected]tchNumber)>0) 
BEGIN 
Print '1 1 1' 
END 

END 

這裏我檢查三個表的條件,如果一個列存在三個表中,它應該打印'1 1 1',如果它只存在於第一個和第三個表中,則打印' 1 0 1'。就像我寫了8條有條件的陳述。但我想要一個簡化的查詢上述存儲過程簡化If和else IF查詢

我想上面的例子最簡單的查詢 - 我該怎麼做?

回答

3

試試這個:如果記錄在表中存在那麼COUNT(DISTINCT 1)會給一個1

declare @prt varchar(10); 
if ((SELECT COUNT(*) FROM tblPacks WHERE [email protected]_ID AND [email protected])=0) 
    set @prt = '0'; 
else 
    set @prt = '1'; 

if ((SELECT COUNT(*) FROM tblBlisters WHERE [email protected]_ID AND [email protected])=0) 
    set @prt = @prt + ' 0'; 
else 
    set @prt = @prt + ' 1'; 

if ((SELECT COUNT(*) FROM tblShippers WHERE [email protected]_ID AND [email protected])=0) 
    set @prt = @prt + ' 0'; 
else 
    set @prt = @prt + ' 1'; 

print @prt; 
2

試試這個

DECLARE @PRINT VARCHAR(10)='' 

SELECT @PRINT = @PRINT+CAST(PR AS VARCHAR(10))+' ' FROM (
SELECT COUNT(DISTINCT 1) AS PR FROM tblPacks WHERE [email protected]_ID AND [email protected] 
UNION ALL 
SELECT COUNT(DISTINCT 1) FROM tblBlisters WHERE [email protected]_ID AND [email protected] 
UNION ALL 
SELECT COUNT(DISTINCT 1) FROM tblShippers WHERE [email protected]_ID AND [email protected] 
)A 

SELECT @PRINT 

,否則零(0)。

接下來將結果集存儲到變量中,方法是將每個記錄添加到變量中。

+0

我認爲這會給開頭的額外空間,例如「1 0 0」代替「1 0 0」 。但是,這可能很容易修復... – takrl

+0

不會。它不會給空間作爲前綴。但空間將在最後。如果我們需要,我們可以刪除。然而尾隨空格不是問題 –

+1

你是對的,這是一個尾隨空間,我誤解了代碼。或者可以通過LTRIM()或RTRIM()輕鬆修復... – takrl

2

我回答是因爲if (select count(*) . . .)是一個非常不好的成語。這需要運行聚合查詢來查看是否存在單個行。更好的方法是if exists()

此外,所有的條件邏輯可以在做了select

declare @prt varchar(10); 

select @prt = (case when exists (select 1 from tblpacks where T_ID = @T_ID and BatchNumber = @BatchNumber) 
        then '0' else '1' 
       end) + 
       (case when exists (select 1 from tblBlisters where T_ID = @T_ID and BatchNumber = @BatchNumber) 
        then ' 0' 
        else ' 1' 
       end) + 
       (case when exists (select 1 from tblShippers where T_ID = @T_ID and BatchNumber = @BatchNumber) 
        then ' 0'; 
        else ' 1' 
       end) 
      ) 
print @prt;