Query连接问题

时间:2013-02-12 23:23:20

标签: sql sql-server-2005 inner-join

我在下面的查询中收到错误。我不确定为什么。

下面的查询是动态sql,它被设置为变量并打印出来。

错误消息:

第150行,第15行,第1行,第60行 关键字“AS”附近的语法不正确。 消息156,第15级,状态1,第77行 关键字“AS”附近的语法不正确。 消息156,第15级,状态1,第86行 关键字“AS”附近的语法不正确。

查询:

    SELECT * FROM(SELECT * FROM (
        SELECT  vwGetMySDDocsLatest.Document_ID,
                vwGetMySDDocsLatest.ViewPrintFlag AS ViewPrint,
                vwGetMySDDocsLatest.ViewOnly,
                vwGetMySDDocsLatest.DownloadFlag AS Download,
                vwGetMySDDocsLatest.DocBookFlag AS DocBook,
                vwGetMySDDocsLatest.Name_ID, 
                vwGetMySDDocsLatest.SheetNo, 
                vwGetMySDDocsLatest.SheetNoTotal, 
                vwGetMySDDocsLatest.Title, 
                vwGetMySDDocsLatest.Rev, 
                vwGetMySDDocsLatest.Rev_Date,
                vwGetMySDDocsLatest.Rev_Purpose_Code, 
                vwGetMySDDocsLatest.[Filename], 
                vwGetMySDDocsLatest.Extension,
                vwGetMySDDocsLatest.DocumentNo,
                vwGetMySDDocsLatest.ClientDocument_No,
                vwGetMySDDocsLatest.DEL_DOCSTRING,
                vwGetMySDDocsLatest.RE_Init,
                vwGetMySDDocsLatest.Project_ID  

            , 1 AS NumberOfRows , 1 AS NumberOfPages 
        ,ROW_NUMBER() OVER(ORDER BY DocumentNo) As RN 
        FROM    vwGetMySDDocsLatest INNER JOIN (SELECT DISTINCT tblSDDocuments_REV.Document_ID FROM tblSDDocuments_REV  WHERE 1 = 1  ) AS DR2 ON vwGetMySDDocsLatest.Document_ID = DR2.Document_ID 
        WHERE vwGetMySDDocsLatest.Name_ID = 4362 AND
        vwGetMySDDocsLatest.Project_ID = 349 AND
        vwGetMySDDocsLatest.DocumentNo Like '%lp-0887-p01%' 
        ) AS newTBL 
                WHERE newTBL.RN BETWEEN 1 AND 25 ) AS onePage 
        INNER JOIN (SELECT tblSDDocuments_Rev.DocumentRev_ID AS R_DocumentRev_ID , 
                        tblSDDocuments_Rev.Document_ID AS R_Document_ID, 
                        tblSDDocuments_Rev.Rev AS R_Rev, 
                        tblSDDocuments_Rev.Rev_Create_Date AS R_Rev_Create_Date, 
                        tblSDDocuments_Rev.Rev_Due_Date AS R_Rev_Due_Date, 
                        dbo.udfNameForward(tblSDDocuments_Rev.Rev_Due_From) AS  R_NameFrom, 
                        tblSDDocuments_Rev.Rev_Date AS R_Rev_Date, 
                        dbo.tblRev_Purpose.Rev_Purpose_Code AS R_Rev_Purpose_Code, 
                        tblSDDocuments_Rev.Flag AS R_Flag,
                        tblSDDocuments_Rev.Filename AS R_Filename, 
                        tblFileExtensions.Extension AS R_Extension,
                        tblSDDocuments_Rev.Client_Filename AS R_Client_Filename,
                        dbo.tblRev_Purpose.Allow_Matrix_View AS R_Allow_Matrix_View,
                        tblSDDocuments_Rev.Filename_Available AS R_Filename_Available,
                        tblSDDocuments_Rev.Filename_Available_Other AS R_Filename_Available_Other,
                        tblRev_Purpose.Rev_Purpose_ID AS R_Rev_Purpose_ID

                        FROM    tblSDDocuments_Rev 
                        LEFT OUTER JOIN tblFileExtensions 
                        ON tblSDDocuments_Rev.Extension_id = tblFileExtensions.Extension_ID 
                        LEFT OUTER JOIN tbl_Name 
                        ON tblSDDocuments_Rev.Rev_Due_From = tbl_Name.Name_Id 
                        LEFT OUTER JOIN dbo.tblRev_Purpose
                        ON tblSDDocuments_Rev.Rev_Purpose_ID = dbo.tblRev_Purpose.Rev_Purpose_ID  WHERE 1 = 1  ) AS TBLRevs

                    ON onePage.Document_ID = TBLRevs.R_Document_ID 
                    INNER JOIN (SELECT Document_ID AS MDID, MAX(DocumentREV_ID) AS MAXREV 
                                FROM tblSDDocuments_REV
                                --WHERE REV_Date IS NOT NULL
                                GROUP BY Document_ID) AS MAXTBL
                    ON MAXTBL.MDID = TBLRevs.R_Document_ID 

                     INNER JOIN ((SELECT Document_ID AS MDID2, MAX(REV_DATE) AS MAXREVDATE
                                FROM tblSDDocuments_REV
                                GROUP BY Document_ID) AS MAX1
                            INNER JOIN tblSDDocuments_REV R1 
                                ON MAX1.MAXREVDATE = R1.Rev_Date AND MAX1.MDID2 = R1.Document_ID)  AS MAXDATETBL
                    ON MAXDATETBL.MDID2 = TBLRevs.R_Document_ID


                    LEFT OUTER JOIN(SELECT DISTINCT td.document_Rev_ID, a.Approval_Abbr
                                    FROM tblsdtransmittal_documents td
                                        inner join tblsdtransmittal t
                                            on td.transmittal_id = t.transmittal_id
                                        inner join tblDocuments_ApprovalType a
                                            ON td.Approval_ID = a.Approval_ID
                                        inner join (
                                            SELECT MAX(t.created_date) AS LatestDate, td.Document_Rev_ID
                                            FROM tblsdtransmittal t
                                                INNER JOIN tblsdtransmittal_documents td
                                                    ON t.transmittal_id = td.transmittal_id
                                            WHERE t.to_supplier = 1
                                            GROUP by  td.Document_Rev_ID ) MAXDATE
                                        ON td.document_Rev_ID = maxdate.document_Rev_ID AND t.created_date = MAXDATE.LatestDate) AS APPRTBL
                    ON tblrevs.r_documentrev_id = APPRTBL.document_Rev_ID


                    LEFT OUTER JOIN(SELECT  SDRevComments_ID AS ETRevComments_ID, 
                            SDRevComments_RevID AS ETRevComments_RevID, 
                            SDRevComments_New_Filename AS ETRevComments_New_Filename, 
                            SDRevComments_Orig_Filename AS ETRevComments_Orig_Filename,
                            SDRevComments_Description AS ETRevComments_Description
                    FROM    tblSDDocuments_Rev_Comments) AS COMTBL
                    ON TBLRevs.R_DocumentREV_ID = COMTBL.ETRevComments_RevID
                     WHERE MAXTBL.MAXREV = TBLRevs.R_DocumentRev_ID   ---check for latest/all revs  

                    ORDER BY    DocumentNo ,TBLRevs.R_Document_ID, TBLRevs.R_DocumentRev_ID DESC

如果我删除了这部分查询:

             INNER JOIN ((SELECT Document_ID AS MDID2, MAX(REV_DATE) AS MAXREVDATE
                        FROM tblSDDocuments_REV
                        GROUP BY Document_ID) AS MAX1
                    INNER JOIN tblSDDocuments_REV R1 
                        ON MAX1.MAXREVDATE = R1.Rev_Date AND MAX1.MDID2 = R1.Document_ID)  AS MAXDATETBL
            ON MAXDATETBL.MDID2 = TBLRevs.R_Document_ID

它运行得很好。我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

你有一个额外的支架(在INNER JOIN之后,松开它并尝试

INNER JOIN 
    (SELECT Document_ID AS MDID2, MAX(REV_DATE) AS MAXREVDATE
    FROM tblSDDocuments_REV
    GROUP BY Document_ID) AS MAX1
    INNER JOIN tblSDDocuments_REV R1 ON MAX1.MAXREVDATE = R1.Rev_Date AND MAX1.MDID2 = R1.Document_ID)  AS MAXDATETBL
ON MAXDATETBL.MDID2 = TBLRevs.R_Document_ID

答案 1 :(得分:0)

问题 与此片段:

 INNER JOIN ((SELECT Document_ID AS MDID2, MAX(REV_DATE) AS MAXREVDATE
            FROM tblSDDocuments_REV
            GROUP BY Document_ID) AS MAX1
        INNER JOIN tblSDDocuments_REV R1 
            ON MAX1.MAXREVDATE = R1.Rev_Date AND MAX1.MDID2 = R1.Document_ID)  AS MAXDATETBL
ON MAXDATETBL.MDID2 = TBLRevs.R_Document_ID

问题是为连接的结果分配别名(MAXDATETBL)。显然SQL Server不喜欢这样,实际上你不需要那个别名。该内部内部联接的结果集中的列应该使用有助于连接的表的别名来引用。也就是说,您应该指定MAXDATETBL.MDID2

而不是MAX1.MDID2
 INNER JOIN ((SELECT Document_ID AS MDID2, MAX(REV_DATE) AS MAXREVDATE
            FROM tblSDDocuments_REV
            GROUP BY Document_ID) AS MAX1
        INNER JOIN tblSDDocuments_REV R1 
            ON MAX1.MAXREVDATE = R1.Rev_Date AND MAX1.MDID2 = R1.Document_ID)  AS MAXDATETBL
ON MAX1.MDID2 = TBLRevs.R_Document_ID

您甚至可以省略内部联接的括号。它们是允许的但是是多余的,因为语法清楚地表明片段开头的INNER JOIN的右侧实际上是MAX1R1之间的连接,而不是{{1}单独。