1

这是问题所在: temprequest 是我从中获取行的表。我在获取时根据 temprequest 表查找可交付物表的 DeliverableId 列。因此我使用 select into 语句。但是我放置查找值的变量仅适用于获取的第一行。

“开始结束”块:

DECLARE no_more_rows BOOLEAN;
DECLARE nr_rows INT DEFAULT 0;  
DECLARE loop_cntr INT DEFAULT 0;

declare delId int;

declare vtname varchar(200);
declare tversion varchar(200);
declare custId int;
declare prod varchar(200);

DECLARE c_temp CURSOR FOR select tname, version,BuID,BuprodName from temprequest;
SET no_more_rows = False;

OPEN c_temp;
select FOUND_ROWS() into nr_rows;

the_loop:LOOP
    FETCH c_temp into vtname,tversion,custId, prod;
    IF no_more_rows THEN            
        LEAVE the_loop;
    END IF;
    -- statements for each request record
    Set delId= (SELECT deliverableId  
                FROM deliverable 
                WHERE deliverable.Product_prodName =vtname AND deliverable.version = tversion);
    INSERT INTO request VALUES (delId, custId, prod);               
    SET loop_cntr = loop_cntr + 1;

END LOOP the_loop;
CLOSE c_temp;
4

2 回答 2

0

如果游标中的任何查询未获取任何结果,则 no_more_rows 变量设置为 true。在您的情况下,如果以下查询未获取任何结果,即使在这种情况下no_more_rows也会评估为true.

SELECT deliverableId  
FROM deliverable 
WHERE deliverable.Product_prodName =vtname AND deliverable.version = tversion

解决这个问题的方法是在游标结束时将 no_more_rows 设置为 false,这样即使在游标执行期间它的计算结果为 true,它也会被重置,并且只有在游标查询没有返回任何行时才会退出。

he_loop:LOOP
    FETCH c_temp into vtname,tversion,custId, prod;
    IF no_more_rows THEN            
        LEAVE the_loop;
    END IF;
    -- statements for each request record
    Set delId= (SELECT deliverableId  
                FROM deliverable 
                WHERE deliverable.Product_prodName =vtname AND deliverable.version = tversion);
    INSERT INTO request VALUES (delId, custId, prod);               
    SET loop_cntr = loop_cntr + 1;
    SET no_more_row = false
END LOOP the_loop;
于 2014-06-18T17:10:02.423 回答
0

要在不使用游标的情况下完成相同的操作(更新):

INSERT INTO request 
SELECT 
    deliverable.deliverableId   as delId,
    qwe.BuID            as custId,
    qwe.BuprodName          as prod

FROM deliverable 
inner join 
(select tname, version, BuID,BuprodName 
from temprequest) 
    as qwe (tname, version, BuID,BuprodName) 
        on deliverable.Product_prodName = qwe.vtname 
       AND deliverable.version = qwe.tversion

我刚刚意识到BuIDandBuprodName是这里的常量,除了插入语句之外没有在任何地方使用。

于 2013-06-07T14:09:11.437 回答