2012-12-27 75 views
0

在兩個表上進行union all之後,現在它忽略了我的WHERE語句的@Badge參數。代碼如下。TSQL忽略UNION後的語句

SELECT  TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
         + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX 

FROM omi.DCUTRX_ZERO 

UNION ALL 

SELECT  TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
         + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX 
FROM omi.TAHIST_ZERO 

WHERE  (ID_BADGE = '  ' + @Badge) AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101')) 

任何幫助非常感謝!

+2

你想把WHERE應用到什麼地方?目前,它適用於第二個查詢。 – Oded

+0

我希望它適用於兩者。我嘗試刪除完全限定的表名並僅使用ID_BADGE,但它返回了所有記錄,而不管ID_BADGE。 – CryptoJones

+0

爲什麼'百分之百'? – Magnus

回答

3

如果你想在WHERE應用到那麼你需要用的語句,並把它應用到結果集,這樣的:如果你想在WHERE子句適用於所有的結果

SELECT * 
FROM (
    SELECT TOP (100) PERCENT ID_BADGE, 
     LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
     DATE_TRX, 
     CODE_TRX 
    FROM omi.DCUTRX_ZERO 
    UNION ALL 
    SELECT TOP (100) PERCENT ID_BADGE, 
     LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
     DATE_TRX, 
     CODE_TRX 
    FROM omi.TAHIST_ZERO 
) q 
WHERE (ID_BADGE = '  ' + @Badge) AND 
    (DATE_TRX BETWEEN @Date AND @Date2) AND 
    (SEQ_REC IN ('0', '1000')) AND 
    (CODE_TRX IN ('100', '101')) 
+0

感謝您的快速響應! – CryptoJones

+0

@CryptoJones,當然,祝你有美好的一天! –

2

,試試這個:

select * from (
SELECT TOP (100) PERCENT 
    ID_BADGE, 
    LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
    DATE_TRX, 
    CODE_TRX 
FROM omi.DCUTRX_ZERO 

UNION ALL 

SELECT TOP (100) PERCENT 
    ID_BADGE, 
    LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
    DATE_TRX, 
    CODE_TRX 
FROM omi.TAHIST_ZERO 
) omi 
WHERE (omi.ID_BADGE = '  ' + @Badge) 
    AND (omi.DATE_TRX BETWEEN @Date AND @Date2) 
    AND (omi.SEQ_REC IN ('0', '1000')) 
    AND (omi.CODE_TRX IN ('100', '101')) 
+0

感謝您的快速響應! – CryptoJones

1

其中應用在過去的UNION SELECT過濾器..

更好的SQL是如下:

SELECT  TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
+ ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX 
FROM omi.DCUTRX_ZERO 
WHERE  (ID_BADGE = '  ' + @Badge) AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101')) 

UNION ALL 

SELECT  TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
        + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX 
FROM omi.TAHIST_ZERO 
WHERE  (ID_BADGE = '  ' + @Badge) AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101')) 
+0

爲什麼這比組合這兩個選擇更好? – CryptoJones

+0

@atheer數據庫足夠聰明,可爲兩個查詢生成相同的計劃。 – Magnus

+0

@Magnus是的你是對的,對於這種情況我現在在SQL Engine 2012中嘗試可以生成相同的執行計劃,在連接之前先應用Filter。 –