FETCH NEXT只读取1行数据

时间:2018-06-02 16:12:28

标签: fetch next

以下代码旨在验证所有订单项的发票分配是否在分配的分类帐中达到100%分配。

我唯一一次收到错误消息,如预期的那样,是发票第一行的字段总和不等于100.

如果第一行的总和等于100,即使发票上的每一行都不符合条件,也不会引发错误消息。

似乎FETCH NEXT只从每张发票上的列表中返回一行,但我不确定原因。

DECLARE @cinvcode       VARCHAR(30)
DECLARE @cinvstatus     VARCHAR(8)
DECLARE @cinvorg        VARCHAR(15)
DECLARE @ctotal         DECIMAL(3,0)

BEGIN
SELECT @cinvcode = inv_code, @cinvstatus = inv_status, @cinvorg = inv_org
FROM invoices
WHERE inv_sqlidentity = :ROWID

IF @cinvstatus = 'R' 
BEGIN
DECLARE c1 CURSOR LOCAL FOR
SELECT SUM (CAST(udfchar16 AS DECIMAL (3,1))) +
SUM (CAST(udfchar17 AS DECIMAL (3,1))) + 
SUM (CAST(udfchar18 AS DECIMAL (3,1))) +  
SUM (CAST(udfchar19 AS DECIMAL (3,1)))
FROM invoicelines 
WHERE invoice = @cinvcode

OPEN c1
FETCH NEXT FROM c1
INTO @ctotal;
WHILE @@FETCH_STATUS = 0
BEGIN

IF @ctotal < 100.0
BEGIN
RAISERROR('Total of all distributions must equal 100', 16, 1);
END

FETCH NEXT FROM c1 
INTO @ctotal;
END
CLOSE c1;
DEALLOCATE c1;

END

END

0 个答案:

没有答案