0

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

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

错误消息:

消息 156,级别 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

它运行良好。知道我在这里做错了什么吗?

4

2 回答 2

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而不是MAX单独连接。

于 2013-02-14T00:54:00.130 回答
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
于 2013-02-12T23:29:35.060 回答