我必須在20個數據庫上運行此查詢才能生成一個統一報告。我之前用UNION做過這個。有沒有什麼方法可以爲每個子查詢重用WITH語句?我收到一個錯誤,說以前的語句必須以分號結尾。有沒有更好的方法來做到這一點?使用WITH語句的SQL UNION
WITH
DUPS (DocNum)
AS (
SELECT DocNum
FROM Akron.dbo.PWZ3
INNER JOIN Akron.dbo.OPWZ T5 ON T5.IdNumber = PWZ3.IdEntry
WHERE T5.PmntDate = '3/10/2011'
GROUP BY DocNum
HAVING COUNT(1) > 1;
)
SELECT PWZ3.IdEntry, PWZ3.DocNum, 'ARK' + PWZ3.CardCode, QUOTENAME(CardName,'"'),
Convert(Decimal(10,2),PayAmount), Convert(Decimal(10,2),InvPayAmnt),
CONVERT(VARCHAR(10), T5.PmntDate,101), NumAtCard, PymMeth, ObjType
FROM Akron.dbo.PWZ3 PWZ3
INNER JOIN DELAWARE.dbo.OPWZ T5 ON T5.IdNumber = PWZ3.IdEntry
LEFT JOIN Dups ON DUPS.DocNum = PWZ3.DocNum
WHERE T5.PmntDate = '3/10/2011'
AND T5.Canceled = 'N'
AND Checked = 'Y'
AND Dups.DocNum is null