在兩個表上進行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'))
任何幫助非常感謝!
你想把WHERE應用到什麼地方?目前,它適用於第二個查詢。 – Oded
我希望它適用於兩者。我嘗試刪除完全限定的表名並僅使用ID_BADGE,但它返回了所有記錄,而不管ID_BADGE。 – CryptoJones
爲什麼'百分之百'? – Magnus