SQL语法错误'联盟'

时间:2018-05-02 08:39:51

标签: sql sql-server sql-server-2008

我不明白为什么给我这个错误。我不写联盟都像这个错误。请给我解决方案。如果你想要这个代码的结果,可以写它。

DECLARE @StartDate DATETIME;

/* WHERE */

SET @StartDate = 

/* T0.TaxDate */

'';

/*SELECT FROM [dbo].[OINV] T1*/

DECLARE @EndDate DATETIME;

/* WHERE */

SET @EndDate = 

/* T1.TaxDate */

'';

/*SELECT FROM [dbo].[OINV] T2*/

DECLARE @OurOffice VARCHAR(MAX);

/* WHERE */

SET @OurOffice = 

/* T2.U_CFFIRMASI */ 

'';

IF(@StartDate = '')
    SET @StartDate =
(
    SELECT TOP 1 T0.TaxDate
    FROM OINV T0
    ORDER BY T0.TaxDate ASC
);
IF(@EndDate = '')
    SET @EndDate =
(
    SELECT TOP 1 T0.TaxDate
    FROM OINV T0
    ORDER BY T0.TaxDate DESC
);
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 RESULT.Country,
       RESULT.[Our Office],
       RESULT.[Inv.Company],
       RESULT.[Team Leader],
       RESULT.[Trade/Comm.],
       RESULT.[Inv. Date],
       RESULT.Customer,
       RESULT.Department,
       RESULT.[Cust.Inv.No],
      RESULT.SoNum,
      RESULT.PONum,
       RESULT.[Mfr. Name],
       RESULT.[Mfr.Inv.No],
       RESULT.Terms,
       RESULT.[Due Date],
       RESULT.[Sales(Original)],
       RESULT.[Currency(S)],
       RESULT.[Cost of Sales(Original)],
       RESULT.[Currency(COS)],
       RESULT.[Sales(Original)] - RESULT.[Cost of Sales(Original)] AS [Gross Profit],
       CAST(CAST(((RESULT.[Sales(Original)] - RESULT.[Cost of Sales(Original)]) / (CASE
                                                                                       WHEN RESULT.[Sales(Original)] = 0
                                                                                       THEN 1
                                                                                       ELSE RESULT.[Sales(Original)]
                                                                                   END)) * 100 AS DECIMAL(19, 2)) AS NVARCHAR)+[%] AS [Margin %],
       RESULT.SOQuantity,
       RESULT.POQuantity,
       RESULT.ARQuantity,
       RESULT.APQuantity,
       RESULT.[Sales(Reporting)],
       RESULT.[Cost of Sales(Reporting)],
       RESULT.[Sales(Reporting)] - RESULT.[Cost of Sales(Reporting)] AS [Gross Profit],
       CAST(CAST(((RESULT.[Sales(Reporting)] - RESULT.[Cost of Sales(Reporting)]) / (CASE
                                                                                    WHEN RESULT.[Sales(Reporting)] = 0
                                                                                    THEN 1
                                                                                    ELSE RESULT.[Sales(Reporting)]
                                                                                END)) * 100 AS DECIMAL(19, 2)) AS NVARCHAR)+[%] AS [Margin %],
       RESULT.DocRate,
       RESULT.Month,
       RESULT.Year,
       RESULT.Week
FROM
(
    SELECT T0.U_URETIMULKESI AS [Country],
           T0.U_CFFIRMASI AS [Our Office],
(
    SELECT A0.CompnyName
    FROM '+@myTempTableName+'.dbo.OADM A0
) AS [Inv.Company],
           T8.Name AS [Team Leader],
           T0.U_ARGNS_OrderType AS [Trade/Comm.],
           T5.TaxDate AS [Inv. Date],
           T0.CardName AS [Customer],
           T9.U_ARGNS_LineCode AS [Department],
           T5.U_FATNO AS [Cust.Inv.No],
           T3.CardName AS [Mfr. Name],
           T11.U_FATNO AS [Mfr.Inv.No],
           T12.PymntGroup AS [Terms],
           T5.DocRate,
           T5.DocDueDate AS [Due Date],
           ((CASE
                 WHEN T4.Currency =
(
    SELECT A0.MainCurncy
    FROM '+@myTempTableName+'.dbo.OADM A0
)
                 THEN T4.LineTotal
                 WHEN T4.Currency =
(
    SELECT A0.SysCurrncy
    FROM '+@myTempTableName+'.dbo.OADM A0
)
                 THEN T4.TotalSumSy
                 ELSE T4.TotalFrgn
             END) * (1 - T0.DiscPrcnt)) AS [Sales(Original)],
           T4.Currency AS [Currency(S)],
           (CASE
                WHEN T6.Currency =
(
    SELECT A0.MainCurncy
    FROM '+@myTempTableName+'.dbo.OADM A0
)
                THEN T6.OpenSum
                WHEN T6.Currency =
(
    SELECT A0.SysCurrncy
    FROM '+@myTempTableName+'.dbo.OADM A0
)
                THEN T6.OpenSumSys
                ELSE T6.OpenSumFC
            END) AS [Cost of Sales(Original)],
           T6.Currency AS [Currency(COS)],
           T1.Quantity AS [SOQuantity],
           T2.Quantity AS [POQuantity],
           T4.Quantity AS [ARQuantity],
           T6.Quantity AS [APQuantity],
           T6.OpenSumSys AS [Cost of Sales(Reporting)],
           T4.OpenSumSys AS [Sales(Reporting)],
           MONTH(T5.TaxDate) AS [Month],
           YEAR(T5.TaxDate) AS [Year],
           DATEPART(wk, T5.TaxDate) AS [Week],
         T0.DocEntry as [SoNum],
         T3.DocEntry as [PoNum]
    FROM '+@myTempTableName+'.dbo.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
         INNER JOIN '+@myTempTableName+'.dbo.INV1 T4 ON T4.BaseEntry = T1.DocEntry
                               AND T4.BaseLine = T1.LineNum
                               AND T4.BaseType = T1.ObjType
         INNER 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.OPCH T11 ON T11.DocEntry = T6.DocEntry
         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.'+@myTempTableName+'.dbo.OITM T9 ON T9.ItemCode = T1.ItemCode
         LEFT JOIN '+@myTempTableName+'.dbo.OUBR T10 ON T10.Code = T7.branch
         LEFT JOIN '+@myTempTableName+'.dbo.OCTG T12 ON T5.GroupNum = T12.GroupNum
    WHERE T0.Canceled = ''N''
          AND T3.CANCELED = ''N''
          AND T5.CANCELED = ''N''
      AND T5.TaxDate BETWEEN @StartDate AND @EndDate
      AND (T5.U_CFFIRMASI = @OurOffice
           OR @OurOffice = '''')
) AS RESULT ) ';
 IF(@loopCounter = 1)
            BEGIN
                SET @sqlInsertCommand = @sqlSingleCommand;
            END;
            ELSE
            BEGIN
                SET @sqlInsertCommand = @sqlInsertCommand+'UNION ALL '+@sqlSingleCommand;
                select @sqlInsertCommand
            END;
        SET @loopCounter = @loopCounter + 1;
    END;

EXECUTE sp_executesql
        @sqlInsertCommand;
  

消息156,级别15,状态1,行103关键字附近的语法不正确   '联盟'。消息102,级别15,状态1,行408语法不正确   '年'。

2 个答案:

答案 0 :(得分:0)

我认为@sqlInsertCommand变量太小了。

尝试将您的查询分成几个变量并更改

EXECUTE sp_executesql
        @sqlInsertCommand;

EXEC('EXEC sp_executesql N''' + @sql1 + @sql2 + @sql3 + @sql4 + '''')

P.S。 编写这样长的动态查询并不是一个好主意。 但是,据我所知,某人可能需要这样的查询。

答案 1 :(得分:0)

我认为你这里有一个充满问题的世界:

  1. 您似乎拥有@StartDate,@ EndDate和@OurOffice的占位符,但是我无法看到您在传入sp_executesql时已将它们声明为参数? (可能是您第一次出错的原因)。
  2. 您正在将nvarchar(80)(@ myTempTableName)连接到nvarchar(max)。这将导致@sqlSingleCommand隐式转换为8000字节的字符串(我相信nvarchar为4000个字符?)有关此行为的示例,请参阅here。 (可能是您第二次错误的原因)。
  3. 在尝试生成@sqlInsertCommand时,第2点也可能适用。
  4. @OurOffice是一个varchar(max),我认为在生成sql命令时会再次导致隐式转换,因为你要将它添加到nvarchar(max)变量中。类似的问题和示例here
  5. 解决方案:

    1. 确保所有变量都已正确声明。 (即nvarchar)。
    2. 您需要将@StartDate,@ EndDate和@OurOffice传入您的sp_executesql调用。
    3. 可选:

      如果您没有解析用户输入到您的代码中,您可以使用EXEC(@sqlcommmand)而不是EXEC sp_executesql,因为EXEC对查询大小没有限制,因为它没有参数化(讨论过) here