2017-09-28 82 views
0

我有這個疑問錯誤查詢

SELECT * 
FROM 
    (SELECT 
     TT.RECID, TT.TRANSDATE, 
     TT.DATAAREAID, TT.SOURCERECID, 
     TT.SOURCECURRENCYCODE, TT.TAXAMOUNT, 
     TT.TAXBASEAMOUNT, TT.TAXBASEAMOUNTCUR, 
     TT.TAXAMOUNTCUR, TT.TAXORIGIN, 
     TT.VOUCHER, TT.TAXITEMGROUP, 
     TT.TAXCODE, TT.SOURCEBASEAMOUNTCUR, 
     TT.SOURCETAXAMOUNTCUR, 
     TTGJAERIVA.TAXTRANSRELATIONSHIP, 
     TTGJAERIVA.GENERALJOURNALACCOUNTENTRY, 
     TTGJAERIVA.LEDGERDIMENSION, 
     GJAERIVA.TEXT, 
     GJAERIVA.LEDGERDIMENSION AS LEDGERDIMENSIONGAE, 
     GJAERIVA.POSTINGTYPE 
    FROM 
     TAXTRANS TT 
    INNER MERGE JOIN 
     TAXTRANSGENERALJOURNALACCOUNTENTRY TTGJAERIVA ON TTGJAERIVA.TAXTRANS = TT.RECID 
    INNER MERGE JOIN 
     GENERALJOURNALACCOUNTENTRY GJAERIVA ON TTGJAERIVA.GENERALJOURNALACCOUNTENTRY = GJAERIVA.RECID 
              AND GJAERIVA.POSTINGTYPE IN (14, 236, 71, 41) 
    WHERE 
     (TT.TAXORIGIN <> 11 
      AND TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30') 
     OR 
     (TT.TAXORIGIN = 11 
      AND TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30')) AS T 

---- --JOIN 
LEFT MERGE JOIN 
    (SELECT 
     SOURCERECID, TAXITEMGROUP, TAXCODE, 
     GENERALJOURNALACCOUNTENTRY, VOUCHER, TAXAMOUNT, 
     TAXAMOUNTCUR 
    FROM 
     T) TTRIVA ON T.VOUCHER = TTRIVA.VOUCHER 
        AND T.SOURCERECID = TTRIVA.SOURCERECID 
        AND TT.TAXITEMGROUP = TTRIVA.TAXITEMGROUP 
        AND (TTRIVA.TAXCODE LIKE 'RIVA%') 
        AND TTRIVA.GENERALJOURNALACCOUNTENTRY = TTGJAE.GENERALJOURNALACCOUNTENTRY 

我得到一個錯誤:

Invalid object name 'T'.

到子查詢,剛剛離開後合併連接(在最後的查詢)

---- --JOIN 
LEFT MERGE JOIN 
    (SELECT 
     SOURCERECID, TAXITEMGROUP, TAXCODE, 
     GENERALJOURNALACCOUNTENTRY, VOUCHER, 
     TAXAMOUNT, TAXAMOUNTCUR 
    FROM 
     T) TTRIVA ON T.VOUCHER = TTRIVA.VOUCHER 
        AND T.SOURCERECID = TTRIVA.SOURCERECID 
        AND TT.TAXITEMGROUP = TTRIVA.TAXITEMGROUP 
        AND (TTRIVA.TAXCODE LIKE 'RIVA%') 
        AND TTRIVA.GENERALJOURNALACCOUNTENTRY = TTGJAE.GENERALJOURNALACCOUNTENTRY 

我不知道爲什麼它會出現,如果我使用'T'作爲查詢結果之前加入?問候

+0

您可能想要分解SQL。這將成爲未來的維護噩夢。 – dhiman

+0

相信我,它現在是一場噩夢@DhimanBhattacharya – Ledwing

+0

備註:特別是在SQL Server上,請[不要使用'BETWEEN'](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/)什麼-DO之間和最魔鬼具有功能於common.aspx)。我不完全相信'transdate'是一個日期,而不是一個完整的時間戳 –

回答

0

一如既往,謹慎的格式對於揭示SQL語句中的錯誤很有用。

SELECT * FROM (
     SELECT 
    TT.RECID, 
    -- etc 
    GJAERIVA.POSTINGTYPE 
    FROM TAXTRANS TT 
    INNER MERGE JOIN TAXTRANSGENERALJOURNALACCOUNTENTRY TTGJAERIVA 
      ON TTGJAERIVA.TAXTRANS = TT.RECID 
    INNER MERGE JOIN GENERALJOURNALACCOUNTENTRY GJAERIVA 
      ON TTGJAERIVA.GENERALJOURNALACCOUNTENTRY = GJAERIVA.RECID 
      AND GJAERIVA.POSTINGTYPE IN(14,236,71,41 
) -- as ???? 
WHERE 
    (TT.TAXORIGIN <> 11 AND 
    TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30') 
OR (TT.TAXORIGIN = 11 AND 
    TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30') 

) AS T 
---- --JOIN 
    LEFT MERGE JOIN --- etc. 

第一WHERE子句前收盤)可能不是你想要的 。

SQL是一種聲明性語言。如果您儘可能清楚地向讀者陳述 ,使用起來會更容易。通過重新格式化您的查詢,比如,我們可以很容易地看到WHERE子句可以減少到

WHERE TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30' 

因爲每個TT.TAXORIGIN將等於11,還是不行。