2014-03-13 85 views
0

我有以下5個表:UNION ALL FROM SELECT語句不工作

TABLE OLD_A (CID, CNO, CNAME) 
TABLE OLD_B (DID, DNO, DNAME) 
TABLE OLD_MAP (CID, DID, TERRITORY) 
TABLE NEW_A (NCID, NCNO, NCNAME) 
TABLE NEW_MAP (NCNO, DNO, TERRITORY ) 

我想CNOCNAME由新的和舊錶,所以我做了下面的查詢

[email protected] varchar(16), @DNO varchar(16) 

SELECT RESULT.`enter code here`CNO as CNO, RESULT.CNAME as CNAME, RESULT.Is_New_Or_Old as Is_New_Or_Old 
FROM 
(
    (SELECT C.CNO as CNO, C.CNAME as CNAME, 'OLD' AS Is_New_Or_Old [OLD_A] AS C LEFT JOIN [OLD_MAP] AS DCL ON C.ID = DCL.CID JOIN [OLD_B] AS D ON DCL.DID = D.ID WHERE C.TERRITORY = @TERRITORY AND D.DNO = @DNO) 
    UNION ALL 
    (SELECT NC.NCNO as CNO, NC.CNAME as CNAME, 'NEW' AS Is_New_Or_Old FROM [NEW_A] AS NC LEFT JOIN [NEW_MAP] AS CDTL ON NC.NCNO = CDTL.NCNO WHERE CDTL.DNO = @DNO AND CDTL.Territory_Id = @TERRITORY) 
) RESULT 
ORDER BY CNO 

但給我錯誤

Incorrect syntax near ')'. 

最後)

回答

2

你以後'OLD' AS Is_New_Or_Old

失蹤FROM這些錯誤可以通過簡單的格式

+0

噢,那是如此愚蠢的錯誤我做了.... – Dhwani

+0

你coludn't看到它是可以避免的,因爲你沒有格式化您的查詢。我建議你總是這樣做 – Alexander

+0

非常感謝。下次會做。 – Dhwani