SAP B1查询返回“必须声明标量变量”错误

时间:2017-07-25 14:10:53

标签: sql sapb1

我正在尝试创建一个下拉菜单,该菜单将使用以下SQL代码限制项目买方的查询结果。但是,我不断收到错误声明我需要声明标量变量@ItemBuyer,尽管事实上它已在代码中明确声明并设置。

    DECLARE @ItemBuyer VARCHAR(30)
    SET @ItemBuyer= /* T3.OwnerCode */ '[%1]'

    DECLARE @SQL VARCHAR(MAX)  
    SET @SQL = 'SELECT T3.[DocNum] AS DocNumber, T3.[CardCode] AS 
    VendorCode, T3.[CardName] AS VendorName, T3.OwnerCode as BuyerID, T2.
    [ItemCode] AS ItemNo, T2.[U_CPM_LegItemNo] AS LegacyItemNumber, T2.
    [Dscription] AS ItemDescription, T2.[U_CPM_ConfDate] AS POConfirmDate, 
    T2.[OpenCreQty] AS CreditMemoAmount FROM  [dbo].[OITG] T0 ,  [dbo].
    [OITM] T1  INNER  JOIN [dbo].[POR1] T2  ON  T2.[ItemCode] = T1.
    [ItemCode]   INNER  JOIN [dbo].[OPOR] T3  ON  T3.[DocEntry] = T2.
    [DocEntry]   WHERE (T2.[OpenCreQty] > (0 ) ) AND  (T2.[U_CPM_ConfDate] 
    IS NULL  ) and (T3.[OwnerCode] = @ItemBuyer)'

    EXEC(@SQL)

我也试过声明并设置如下变量:

    DECLARE @ItemBuyer VARCHAR(30) = /* T3.[OwnerCode] */ '[%0]'

但是后来我收到错误声明语法错误,即使变量现在返回正确的值。我有点卡在这里。希望有人可以帮助我。

谢谢,

Krys

1 个答案:

答案 0 :(得分:1)

我会像这样重写查询:

/*SELECT FROM [dbo].[OPOR] P1*/
declare @ItemBuyer as int
/* WHERE */
set @ItemBuyer = /* P1.OwnerCode */ '[%0]'


SELECT T3.[DocNum] AS DocNumber
, T3.[CardCode] AS VendorCode
, T3.[CardName] AS VendorName
, T3.OwnerCode as BuyerID
, T2.[ItemCode] AS ItemNo
, T2.[U_CPM_LegItemNo] AS LegacyItemNumber
, T2.[Dscription] AS ItemDescription
, T2.[U_CPM_ConfDate] AS POConfirmDate
, T2.[OpenCreQty] AS CreditMemoAmount 
FROM [dbo].[POR1] T2  
INNER JOIN [dbo].[OPOR] T3 ON T3.[DocEntry] = T2.[DocEntry]
WHERE T2.[OpenCreQty] > 0 
AND T2.[U_CPM_ConfDate] IS NULL 
and T3.[OwnerCode] = @ItemBuyer

我仍然无法记住动态语法查询,因此我每次都要使用此链接:http://www.clientsfirst-us.com/blog/sap/sap-business-one/user-defined-prompts-in-sap-business-one-queries/

另请注意,我删除了您在selectwhere语句中未使用的几个表格。它可能不会在查询速度上产生很大差异,但它只是使它更清晰。 (我也清理了格式,把它放在我喜欢的样式中,但这并不是绝对必要的)