While Loop SAP B1 SQL 阻塞存储过程

While Loop SAP B1 SQL stored procedure for blocking

我的存储过程 SAP B1 有问题。

我在这里要做的是存储数量的总和,按制造订单分组并将其插入临时文件 table。然后使用 while 循环遍历每个 ID 以与用户 table [@FGTRACKING] 进行比较并阻止如果 temp.quantity > [@FGTracking] 中的总和(数量)。

但是这不起作用,事务仍然通过了存储过程块。我怀疑我的语法有问题。

IF @transaction_type IN ('A') AND @object_type = '67'
    BEGIN
    declare @top as int
    declare @temp table (id int,quantity int NOT NULL, monum int NOT NULL)
    insert into @temp (id, quantity,monum)
    select row_number() over (order by (select NULL)), sum(quantity) as quantity, u_shipment_line as monum
    from wtr1 t1
    where t1.docentry = @list_of_cols_val_tab_del
    group by u_shipment_line
    set @top = 1
    WHILE @top <= (select count(monum) from @temp)
    BEGIN
    IF EXISTS (select t100.monum from @temp t100 
    where t100.quantity > (select sum(t111.u_transfer) 
    from [@FGTRACKING] t111 where t111.u_mo_num = t100.monum 
    group by t111.u_mo_num) and t100.id = @top)
    BEGIN
    SELECT @Error = 666, @error_message = 'Over-transfer'
    END
    ELSE
    set @top = @top + 1
    END
    END

看起来您只是在没有遇到错误条件时递增迭代器 (@top),因此如果错误条件触发,您将陷入无限循环。

摆脱你的 "else" 并始终递增 @top,或者当你遇到错误条件时跳出你的 while 循环。

...
ELSE  -- Get rid of this else
set @top = @top + 1
...