0

我正在尝试做一些事情,例如:

for(int i = 0; i<10; i++)
{
    for(int j = 0; j<10; j++)
    {
        Blah;
    }
}

//As you can see each time that there is a different i, j starts at 0 again.

在 Oracle 中使用游标。但如果我是正确的,在我从游标中获取所有行之后,它不会重新启动。有没有办法做到这一点?

这是我的sql:

CREATE OR REPLACE PROCEDURE SSACHDEV.SyncTeleappWithClientinfo
as
teleCase NUMBER;

CURSOR TeleAppCursor
is
    Select 
        distinct(casenbr)
    from TeleApp;


CURSOR ClientInfoCursor
is
    Select casenbr 
    from clientinfo 
    where trim(cashwithappyn) is null;

BEGIN

    open TeleAppCursor;
    open ClientInfoCursor;

    LOOP
        fetch TeleAppCursor into teleCase;
        EXIT when TeleAppCursor%NOTFOUND;

        LOOP
            fetch ClientInfoCursor into clientCase;
            EXIT when ClientInfoCursor%NOTFOUND;
                if clientCase = teleCase then

                    update ClientInfo 
                    set cashwithappyn = (select cashwithappyn from teleapp where casenbr = clientCase) 
                    where casenbr = clientCase;

                    break;
                end if;
        END LOOP;
    END LOOP;

END;

我确实在网上查过,但在这方面找不到任何东西。

4

2 回答 2

3

您可以使用表变量来存储 sql 语句的结果,然后在表上循环任意次数,而不是重新启动游标。

这是使用 SQL Fiddle 示例数据的示例。

DECLARE 
    CURSOR c1 IS 
      SELECT id, 
             TYPE, 
             details 
      FROM   supportcontacts; 

    TYPE contactrec 
      IS TABLE OF c1%ROWTYPE INDEX BY BINARY_INTEGER; 

    acontact    c1%ROWTYPE; 
    contactlist CONTACTREC; 
    counter     INTEGER; 
BEGIN 
    counter := 0; 

    OPEN c1; 

    LOOP 
        FETCH c1 INTO acontact; 

        IF c1%FOUND THEN 
          counter := counter + 1; 
        END IF; 

        Contactlist(counter) := acontact; 

        IF c1%NOTFOUND THEN 
          EXIT; 
        END IF; 
    END LOOP; 

    CLOSE c1; 



    FOR i IN 1..5 LOOP 
        FOR j IN 1..counter LOOP 
            dbms_output.Put_line(Contactlist(j).type  || ' ' || Contactlist(j).details); 
        END LOOP; 
    END LOOP; 
END; 

/ 

哪个输出

Email admin@sqlfiddle.com
Twitter @sqlfiddle
Email admin@sqlfiddle.com
Twitter @sqlfiddle
Email admin@sqlfiddle.com
Twitter @sqlfiddle
Email admin@sqlfiddle.com
Twitter @sqlfiddle
Email admin@sqlfiddle.com
Twitter @sqlfiddle

这是SQL Fiddle,但我不知道如何查看 dbms_output 的输出

于 2012-09-27T22:15:20.017 回答
0

您根本不需要第二个游标,只需使用 Oracle 中的 set 操作来更新相应的记录,而无需自己手动搜索:

DECLARE
    v_teleCase      TeleApp.teleCase%TYPE;
    v_cashwithappyn TeleApp.cashwithappyn%TYPE

    CURSOR TeleAppCursor
    is
        Select 
            distinct casenbr, cashwithappyn
        from TeleApp;

BEGIN

    open TeleAppCursor;

    LOOP
        fetch TeleAppCursor into v_teleCase, v_cashwithappyn;
        EXIT when TeleAppCursor%NOTFOUND;

        UPDATE ClientInfo
        SET cashwithappyn = v_cashwithappyn
        WHERE casenbr = v_teleCase
        AND trim(cashwithappyn) is null;

    END LOOP;

END;

最好不要有与列同名的变量。

于 2012-09-27T22:29:09.800 回答