2013-08-06 48 views
0

我試圖運行對在水晶報表的AS/400的DB2數據庫這個SQL,我得到一個錯誤SQL爲DB2在晶體不能正常工作報告

FAILED TO RETREIVE DATA FROM THE DATABASE" Details 42000 IBM ISeries ACCESS ODBC Driver DB2 UDB SQL0104 Token Not valid. Valid tokens <End of Statement> Database Vendor code 104

(SELECT LMLTPC, COALESCE(IRLOC1,'') as IRLOC1, COALESCE(IRLOC2,'') 
as IRLOC2, COALESCE(IRLOC3,'') as IRLOC3, IRPRT#, IRQOH#, IRWHS#, 
'' as IEPRT#, '.00' as IEQOH#, '' as IELOC1, '' as IELOC2, '' as 
IELOC3, '' as IEWHS# 
FROM 
(SELECT LMLTPC, LMLOC1, LMLOC2, LMLOC3 FROM ASTDTA.ICLOCMLM WHERE 
LMLTPC='PAL') t1 
left outer join 
(SELECT IRLOC1, IRLOC2, IRLOC3, IRPRT#, IRQOH#, IRWHS# FROM 
ASTDTA.ICBLDTIR ) t2 
On LMLOC1=IRLOC1 AND LMLOC2=IRLOC2 AND LMLOC3=IRLOC3) 
UNION ALL 
(SELECT ' ' as LMLTPC, ' ' as IRLOC1, ' ' as IRLOC2, ' ' as IRLOC3, 
'' as IRPRT#, '.00' as IRQOH#, '' as IRWHS#, IEPRT#, IEQOH#, IELOC1, 
IELOC2, IELOC3, 
IEWHS# FROM ASTDTA.ICBALMIE)) 
+0

最後的封閉括號沒有匹配的開括號。這是我注意到的第一件事。你可以嘗試刪除並再次嘗試? – zedfoxus

+0

YE我做到了。現在有一個不同的錯誤:SQL0199關鍵字外部不是預期UNION EXCEPT(DATABASE VENDOR CODE -199) –

+0

而不是使用左外部聯接,你可以嘗試使用左連接?或者,嘗試在我的答案中使用重寫的查詢,並查看是否出現錯誤。 – zedfoxus

回答

3

最後結束括號應該是違規的。刪除它,以便您的代碼如下所示:

... 
... 
IEWHS# FROM ASTDTA.ICBALMIE) 

我使用AS/400 v6r1系統上的虛擬數據進行了測試。 SQL將編譯並生成沒有問題的數據。希望Crystal Reports在使用結果數據方面不會有太多問題。我無法訪問Crystal Reports來測試結果。

只有當你有興趣不請自來的提醒,我建議寫SQL的方式略有不同:

SELECT 
     LMLTPC, 
     COALESCE(IRLOC1,'') as IRLOC1, 
     COALESCE(IRLOC2,'') as IRLOC2, 
     COALESCE(IRLOC3,'') as IRLOC3, 
     IRPRT#, 
     IRQOH#, 
     IRWHS#, 
     '' as IEPRT#, 
     '.00' as IEQOH#, 
     '' as IELOC1, 
     '' as IELOC2, 
     '' as IELOC3, 
     '' as IEWHS# 
FROM ASTDTA.ICLOCMLM mlm 
left join ASTDTA.ICBLDTIR tir 
     on mlm.LMLOC1 = tir.IRLOC1 
     and mlm.LMLOC2 = tir.IRLOC2 
     and mlm.LMLOC3 = tir.IRLOC3 
where LMLTPC = 'PAL' 

UNION ALL 

SELECT 
    ' ' as LMLTPC, 
    ' ' as IRLOC1, 
    ' ' as IRLOC2, 
    ' ' as IRLOC3, 
    '' as IRPRT#, 
    '.00' as IRQOH#, 
    '' as IRWHS#, 
    IEPRT#, 
    IEQOH#, 
    IELOC1, 
    IELOC2, 
    IELOC3, 
    IEWHS# 
FROM ASTDTA.ICBALMIE 
+1

良好的格式使得查找語法錯誤更容易。 – WarrenT