我想创建一个如下所示的存储过程
create or replace PROCEDURE AAA
(
cv_1 IN OUT SYS_REFCURSOR
)
as
BEGIN
DECLARE ord_t VARCHAR2(20);
begin
SELECT order_type into ord_t from BT;
dbms_output.Put_line(ord_t);
end;
if ord_t = 'xxx' THEN
open cv_1 for
select * from BA;
else
open cv_1 for
select * from BB;
END if;
END;
但是有一个错误消息说必须声明ord_t。我该如何正确写呢? proc的目的是根据ord_t的值从BA或BB返回结果。
答案 0 :(得分:3)
您需要在使用BEGIN标签之前声明该变量
create or replace PROCEDURE AAA(cv_1 IN OUT SYS_REFCURSOR)
as
ord_t VARCHAR2(20);
BEGIN
SELECT order_type into ord_t from BT;
dbms_output.Put_line(ord_t);
if ord_t = 'xxx' THEN
open cv_1 for
select * from BA;
else
open cv_1 for
select * from BB;
END if;
END;
在过程中,CREATE OR REPLACE PROCEDURE name(params) AS
替换块的DECLARE
。因此,您在这里不需要它。
块的定义是
DECLARE or CREATE FUNCTION/PROCEDURE
(variables in block)
BEGIN
(code using variables)
END
示例:
declare
myvar number(1);
begin
--begin scope of myvar--
select 1 into myvar from dual;
--end scope of myvar--
end;
create procedure myproc (var1 in number)
as
myvarouter number(1);
begin
--begin scope myvarouter--
declare
myvarinner number(1);
begin
--begin scope myvarinner--
select 1 into myvarinner from dual;
select 1 into myvarouter from dual;
--end scope myvarinner--
end;
select 1 into myvarouter from dual;
--end scope myvarouter--
end;
也可以找到另一个内容相同的问题here