sql顺序不正确的语法

时间:2018-04-30 06:31:27

标签: sql sql-server sql-order-by

DECLARE @MyTemp TABLE
(DatabaseName VARCHAR(MAX),
 RowNumber    INT
);
INSERT INTO @MyTemp
(RowNumber,
 DatabaseName
)
       SELECT RowNumber,
              DatabaseName
       FROM QUESINTERNATIONALCORP.dbo.GETVOLUMEQTYDATABASES();
DECLARE @loopLimit INT=
(
    SELECT TOP 1 RowNumber
    FROM @MyTemp
    ORDER BY RowNumber DESC
);
DECLARE @loopCounter INT= 1;
DECLARE @sqlInsertCommand NVARCHAR(MAX);
DECLARE @sqlSingleCommand NVARCHAR(MAX);
DECLARE @myTempTableName NVARCHAR(80);
DECLARE @SystemCurr NVARCHAR(3);
DECLARE @MainCurr NVARCHAR(3);
DECLARE @myTempleTableRealName NVARCHAR(MAX);
DECLARE @SystemCurrSql NVARCHAR(MAX);
DECLARE @MainCurrSql NVARCHAR(MAX);
DECLARE @myTempleTableRealNameSQL NVARCHAR(MAX);
WHILE(@loopCounter <= @loopLimit)
    BEGIN
        SET @myTempTableName =
(
    SELECT DatabaseName
    FROM @MyTemp
    WHERE RowNumber = @loopCounter
);
        SET @SystemCurrSql = N'SELECT @SystemCurrOut = 
    SysCurrncy
    FROM '+@myTempTableName+'.dbo.OADM';
        SET @MainCurrSql = N'SELECT @MainCurrOut = 
    MainCurncy
    FROM '+@myTempTableName+'.dbo.OADM';
        SET @myTempleTableRealNameSQL = N'SELECT @MainNameOut = 
    CompnyName
    FROM '+@myTempTableName+'.dbo.OADM';
        EXECUTE sp_executesql
                @SystemCurrSql,
                N'@SystemCurrOut NVARCHAR(3) OUTPUT',
                @SystemCurrOut = @SystemCurr OUTPUT;
        EXECUTE sp_executesql
                @MainCurrSql,
                N'@MainCurrOut NVARCHAR(3) OUTPUT',
                @MainCurrOut = @MainCurr OUTPUT;
        EXECUTE sp_executesql
                @myTempleTableRealNameSQL,
                N'@MainNameOut NVARCHAR(MAX) OUTPUT',
                @MainNameOut = @myTempleTableRealName OUTPUT;
        SET @sqlSingleCommand = '
(SELECT T0.DocEntry AS ''Order Id'',
       T0.CardName AS ''Customer Name'',
       T3.CardName AS ''Manufacturer Name'',
       T1.OpenQty AS ''Remain Order Qty'',
       ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate) AS ''Revised Ship Date'',
       DATEPART(wk, ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS ''Week'',
       YEAR(ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS ''Year''
FROM ORDR T0
     INNER JOIN '+@myTempTableName+'.dbo.RDR1 T1 ON T1.DocEntry = T0.DocEntry
     INNER JOIN '+@myTempTableName+'.dbo.POR1 T2 ON T2.BaseEntry = T1.DocEntry
                           AND T2.BaseLine = T1.LineNum
                           AND T2.BaseType = T1.ObjType
     INNER JOIN '+@myTempTableName+'.dbo.OPOR T3 ON T3.DocEntry = T2.DocEntry
     LEFT JOIN '+@myTempTableName+'.dbo.INV1 T4 ON T4.BaseEntry = T1.DocEntry
                          AND T4.BaseLine = T1.LineNum
                          AND T4.BaseType = T1.ObjType
     LEFT JOIN '+@myTempTableName+'.dbo.OINV T5 ON T5.DocEntry = T4.DocEntry
     LEFT JOIN '+@myTempTableName+'.dbo.PCH1 T6 ON T6.BaseEntry = T2.DocEntry
                          AND T6.BaseLine = T2.LineNum
                          AND T6.BaseType = T2.ObjType
     LEFT JOIN '+@myTempTableName+'.dbo.OHEM T7 ON T7.empID = T0.OwnerCode
     LEFT JOIN '+@myTempTableName+'.dbo.OUDP T8 ON T8.Code = T7.dept
     LEFT JOIN '+@myTempTableName+'.dbo.OITM T9 ON T9.ItemCode = T1.ItemCode
     LEFT JOIN '+@myTempTableName+'.dbo.OUBR T10 ON T10.Code = T7.branch
WHERE T0.Canceled = ''N''
      AND T3.CANCELED = ''N''
      AND T1.LineStatus = ''O''
ORDER BY T0.DocEntry) ';
IF(@loopCounter = 1)
            BEGIN
                SET @sqlInsertCommand = @sqlSingleCommand;
            END;
            ELSE
            BEGIN
                SET @sqlInsertCommand = @sqlInsertCommand+'UNION ALL '+@sqlSingleCommand;

            END;
        SET @loopCounter = @loopCounter + 1;
    END;

EXECUTE sp_executesql
        @sqlInsertCommand;

是我的错误:

  

(受影响的4行)消息156,级别15,状态1,行29不正确   关键字'ORDER'附近的语法。
消息156,级别15,状态1,行   57
关键字'ORDER'附近的语法不正确。
消息156,级别   15,State 1,Line 85
关键字附近的语法不正确   'ORDER'。
消息156,级别15,状态1,行113
语法不正确   在关键字'ORDER'附近。

这是我的结果:

(SELECT T0.DocEntry AS 'Order Id',
       T0.CardName AS 'Customer Name',
       T3.CardName AS 'Manufacturer Name',
       T1.OpenQty AS 'Remain Order Qty',
       ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate) AS 'Revised Ship Date',
       DATEPART(wk, ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS 'Week',
       YEAR(ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS 'Year'
FROM ORDR T0
     INNER JOIN CHERRYFIELDGMBHCOKG.dbo.RDR1 T1 ON T1.DocEntry = T0.DocEntry
     INNER JOIN CHERRYFIELDGMBHCOKG.dbo.POR1 T2 ON T2.BaseEntry = T1.DocEntry
                           AND T2.BaseLine = T1.LineNum
                           AND T2.BaseType = T1.ObjType
     INNER JOIN CHERRYFIELDGMBHCOKG.dbo.OPOR T3 ON T3.DocEntry = T2.DocEntry
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.INV1 T4 ON T4.BaseEntry = T1.DocEntry
                          AND T4.BaseLine = T1.LineNum
                          AND T4.BaseType = T1.ObjType
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.OINV T5 ON T5.DocEntry = T4.DocEntry
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.PCH1 T6 ON T6.BaseEntry = T2.DocEntry
                          AND T6.BaseLine = T2.LineNum
                          AND T6.BaseType = T2.ObjType
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.OHEM T7 ON T7.empID = T0.OwnerCode
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.OUDP T8 ON T8.Code = T7.dept
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.OITM T9 ON T9.ItemCode = T1.ItemCode
     LEFT JOIN CHERRYFIELDGMBHCOKG.dbo.OUBR T10 ON T10.Code = T7.branch
WHERE T0.Canceled = 'N'
      AND T3.CANCELED = 'N'
      AND T1.LineStatus = 'O'
**ORDER BY T0.DocEntry) UNION ALL** 


(SELECT T0.DocEntry AS 'Order Id',
       T0.CardName AS 'Customer Name',
       T3.CardName AS 'Manufacturer Name',
       T1.OpenQty AS 'Remain Order Qty',
       ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate) AS 'Revised Ship Date',
       DATEPART(wk, ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS 'Week',
       YEAR(ISNULL(T0.U_CH_RevExDate, T0.U_CH_CustExDate)) AS 'Year'
FROM ORDR T0
     INNER JOIN CHERRYFIELDTRADINGLTD.dbo.RDR1 T1 ON T1.DocEntry = T0.DocEntry
     INNER JOIN CHERRYFIELDTRADINGLTD.dbo.POR1 T2 ON T2.BaseEntry = T1.DocEntry
                           AND T2.BaseLine = T1.LineNum
                           AND T2.BaseType = T1.ObjType
     INNER JOIN CHERRYFIELDTRADINGLTD.dbo.OPOR T3 ON T3.DocEntry = T2.DocEntry
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.INV1 T4 ON T4.BaseEntry = T1.DocEntry
                          AND T4.BaseLine = T1.LineNum
                          AND T4.BaseType = T1.ObjType
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.OINV T5 ON T5.DocEntry = T4.DocEntry
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.PCH1 T6 ON T6.BaseEntry = T2.DocEntry
                          AND T6.BaseLine = T2.LineNum
                          AND T6.BaseType = T2.ObjType
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.OHEM T7 ON T7.empID = T0.OwnerCode
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.OUDP T8 ON T8.Code = T7.dept
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.OITM T9 ON T9.ItemCode = T1.ItemCode
     LEFT JOIN CHERRYFIELDTRADINGLTD.dbo.OUBR T10 ON T10.Code = T7.branch
WHERE T0.Canceled = 'N'
      AND T3.CANCELED = 'N'
      AND T1.LineStatus = 'O'
ORDER BY T0.DocEntry) 

如何解决它

1 个答案:

答案 0 :(得分:2)

ORION BY子句对于UNION ALL无效,您可以谷歌并找到解决此问题的方法