在pl/sql存储过程中声明游标

u4vypkhs  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(461)

printf('当地时间问候语');
在使用sql developer运行存储过程时遇到一些问题。下面是一个模式与前几行完全相同的示例,出于安全原因更改了变量名。

CREATE OR REPLACE PROCEDURE redacted ( an_in_variable IN VARCHAR ) AS
    these VARCHAR;
    variables VARCHAR;
    don_apostrophe_t INT;
    matter INT;
BEGIN

DECLARE cursor_giving_me_trouble CURSOR FOR
SELECT something FROM db.table WHERE condition_1 = condition_2;
...

在编辑器中,select单词是红色的波浪线,当我尝试运行代码时,输出返回

PLS-00103: Encountered symbol "FOR" when expecting one of the following     := . ( @ % ; not null range default character

有什么想法吗?

7rfyedvj

7rfyedvj1#

你需要使用 IS 而不是 FOR ,但定义中的术语顺序也不正确:

DECLARE
  CURSOR cursor_giving_me_trouble IS
    SELECT something FROM db.table WHERE condition_1 = condition_2;

db<>fiddle由于模糊代码中的非法对象名(而不是语法)仍然会出错;还有一个更完整的例子。
也有可能您试图使用构造:

FOR cursor_giving_me_trouble IN (
    SELECT something FROM db.table WHERE condition_1 = condition_2
) LOOP
...

而不是一个子块(一个新的declare,后跟begin/end),其中只引用游标。db<>小提琴。

afdcj2ne

afdcj2ne2#

为了让您开始:

create or replace procedure redacted
    ( an_in_variable in varchar2 )
as
    these varchar2(123);
    variables varchar2(456);
    don_apostrophe_t integer;
    matter integer;
    cursor cursor_giving_me_trouble is
        select 'Welcome to PL/SQL' as whatever from dual where 1=1;
begin
    for r in cursor_giving_me_trouble loop
        dbms_output.put_line(r.whatever);
    end loop;
end;

但是,您通常不需要单独的游标定义,因为有以下紧凑的语法:

create or replace procedure redacted
    ( an_in_variable in varchar2 )
as
    these varchar2(123);
    variables varchar2(456);
    don_apostrophe_t integer;
    matter integer;
begin
    for r in (
        select 'Welcome to PL/SQL' as whatever from dual where 1=1
    )
    loop
        dbms_output.put_line(r.whatever);
    end loop;
end;

相关问题