2017-05-04 68 views
0

我們在我們的SAP Business One程序中查詢了一位前同事創建的程序。SQL查詢錯誤 - 包含的引號和錯誤的語法

查詢自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),'[email protected]+',112) 
 
and convert(varchar(15),T0.Docdate,112) <= convert(varchar(15),'[email protected]+',112) 
 
and t0.docentry not in ('[email protected]+') 
 
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),'[email protected]+',112) 
 
and convert(varchar(15),T0.Docdate,112) <= convert(varchar(15),'[email protected]+',112) 
 
and t0.docentry not in ('[email protected]+') 
 
group by T0.CardName ,T4.ItmsGrpNam, T0.CardCode, T6.[GroupName] 
 

 
    
 
) src 
 
PIVOT (sum(Sal) for ItmsgrpNam IN ('[email protected]+')) AS pvt' 
 
EXECUTE (@Query)

任何人都可以建議我怎麼能着手解決這個或看到任何明顯的錯誤代碼?

+0

當時移除一個零件以找出問題零件的位置。 – jarlh

+0

看起來你正在SQL上運行SAP,你有沒有試過在SSMS中運行查詢?你將不得不改變它一點,以擺脫SAP特定的任務'SET @FROM ='和'SET @TO =' –

回答

0

此查詢所做的工作是在@query中構建一個動態查詢字符串,並在最後執行。在查詢的某個地方,它添加了一個具有未封閉引號的值。

而不是有EXEC(@query),您可以使用SELECT @query返回動態查詢字符串,檢查它,找到它出錯的地方,並修復有問題的記錄。

我的猜測是在OITB.ItmsGrpNam的項目組名稱中的一箇中有一個引號,將需要刪除或正確轉義。