PL/SQL循环通过游标

Cha*_*rts 8 sql plsql loops cursor

我的问题不是太复杂,但我是PL/SQL的新手.

我需要根据某些条件从COMPANIES表中进行选择.然后我需要遍历这些并将一些字段转换为不同的格式(我为此创建了函数),最后使用此转换后的版本连接到引用表以获取我需要的分数变量.所以基本上:

select id, total_empts, bank from COMPANIES where turnover > 100000 
Run Code Online (Sandbox Code Playgroud)

循环选择此选项

insert into MY_TABLE (select score from REF where conversion_func(MY_CURSOR.total_emps) =  REF.total_emps)
Run Code Online (Sandbox Code Playgroud)

这基本上就是我要做的.它稍微复杂一点,但我只是在寻找基础知识,以及如何处理它让我入门!

kur*_*sch 13

这是PL/SQL中游标循环的基本语法:

BEGIN

    FOR r_company IN (
        SELECT
            ID,
            total_emps,
            bank
        FROM
            companies
        WHERE
            turnover > 100000
    ) LOOP

        INSERT INTO 
            my_table
        SELECT
            score
        FROM
            ref_table
        WHERE
            ref.total_emps = conversion_func( r_company.total_emps )
        ;

    END LOOP;

END;
/
Run Code Online (Sandbox Code Playgroud)