SQL查询错误 - 包含的引号和不正确的语法

时间:2017-05-04 09:34:47

标签: sql sap sapb1

我们在SAP Business One计划中有一个查询,这是一位前同事创建的。

自2015年创建查询以来,该查询每个月都运行良好,但今天却出现了错误,我们这里没有SQL专家。

错误:

1). [Microsoft][SQL Server Native Client 11.0][SQL Server]Unclosed quotation mark after the character string 'SALES PROMOT'. 2). [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'SALES PROMOT'.  'Document' (RDOC)

查询:



DECLARE @listCol VARCHAR(8000)
DECLARE @Query VARCHAR(8000)
DECLARE @OINV VARCHAR(8000)
DECLARE @ORIN VARCHAR(8000)
DECLARE @From varchar(15)
DECLARE @To varchar(15)
Set @From =/* SELECT convert(varchar(15),Max(T0.DocDate),112) FROM OINM T0 WHERE T0.DocDate > */ convert(varchar(15),'[%0]',112)
Set @To =/* SELECT convert(varchar(15),Max(T0.DocDate),112) FROM OINM T0 WHERE T0.DocDate > */ convert(varchar(15),'[%1]',112)
 
 
SET @OINV =  isnull( STUFF((Select  ',' + convert(varchar(10),Docentry) from OINV
  where CANCELED <>'N' FOR XML PATH('')),1,1,'') ,0)
SET @ORIN =  isnull(STUFF((Select  ',' + convert(varchar(10),Docentry) from ORIN
  where CANCELED <>'N' FOR XML PATH('')),1,1,'') ,0)
SELECT  @listCol = STUFF(( SELECT DISTINCT '],[' + ltrim((ItmsGrpNam)) from OITB
ORDER BY '],[' + ltrim((ItmsgrpNam)) FOR XML PATH('') ), 1, 2, '') + ']'
 
SET @Query = 'Select *  from (Select T0.CardCode, T0.CardName, T6.[GroupName], T4.ItmsGrpNam,sum(T1.LineTotal) as Sal from
OINV T0 Inner join INV1 T1 on T0.DocEntry = T1.DocEntry
Inner Join OFPR T2 on T2.AbsEntry = T0.FinncPriod
Inner Join OITM T3 Inner Join OITB T4 On T4.ItmsGrpCod = T3.ItmsgrpCod on T1.ItemCode = T3.ItemCode Inner Join OCRD T5 On T5.CardCode=T0.CardCode inner join OCRG T6 On T6.[GroupCode]=T5.[GroupCode]
Where convert(varchar(15),T0.Docdate,112) >= convert(varchar(15),'+@From+',112)
and convert(varchar(15),T0.Docdate,112) <= convert(varchar(15),'+@To+',112)
and t0.docentry not in ('+@OINV+')
group by T0.CardName ,T4.ItmsGrpNam,T0.CardCode, T6.[GroupName]
 
union all
 
Select T0.CardCode, T0.CardName, T6.[GroupName], T4.ItmsGrpNam,sum(-T1.LineTotal) as Sal from
ORIN T0 Inner join RIN1 T1 on T0.DocEntry = T1.DocEntry
Inner Join OFPR T2 on T2.AbsEntry = T0.FinncPriod
Inner Join OITM T3 Inner Join OITB T4 On T4.ItmsGrpCod = T3.ItmsgrpCod on T1.ItemCode = T3.ItemCode Inner Join OCRD T5 On T5.CardCode=T0.CardCode inner join OCRG T6 On T6.[GroupCode]=T5.[GroupCode]
Where convert(varchar(15),T0.Docdate,112) >= convert(varchar(15),'+@From+',112)
and convert(varchar(15),T0.Docdate,112) <= convert(varchar(15),'+@To+',112)
and t0.docentry not in ('+@ORIN+')
group by T0.CardName ,T4.ItmsGrpNam, T0.CardCode, T6.[GroupName]

 
) src
PIVOT (sum(Sal) for ItmsgrpNam IN ('+@listCol+')) AS pvt'
EXECUTE (@Query)
&#13;
&#13;
&#13;

有人可以建议我如何开始对此进行故障排除或查看代码中的任何明显错误吗?

0 个答案:

没有答案