不好意思,我第一个存储过程复制错了,没有双引号,都是单引号create or replace procedure p_test2(t1 in varchar2,t2 in varchar2,t3 in number) astmpsql varchar2(1000); t3_toolarge exception; t3_toosmall exception; begintmpsql:=' case when t3>100 then raise t3_toolarge; when t3<50 then raise t3_toosmall; end case; ';execute immediate tmpsql;--tmpsql:=''s''; --execute immediate tmpsql; dbms_output.put_line('hello'); exception when t3_toolarge then dbms_output.put_line('too large'); when t3_toosmall then dbms_output.put_line('too small');end p_test2;
tmpsql:=' -- 没有内容呀!? case when t3>100 then raise t3_toolarge; when t3<50 then raise t3_toosmall; end case; ';execute immediate tmpsql; --?!
to lianhg(lianhg) : 我写此段过程的目的是为了试用一下过程中的错误处理以及case是否必须写成动态sql。 t3>100为过大,t3<50为过小。兄台你看能否给写个例子看看,我对oracle很陌生。我的oracle版本为817。
SQL> create or replace procedure p_test2(t1 in varchar2,t2 in varchar2,t3 in number 2 as 3 tmpsql varchar2(1000); 4 t3_toolarge exception; 5 t3_toosmall exception; 6 begin 7 if t3>100 then 8 raise t3_toolarge; 9 end if; 10 if t3<50 then 11 raise t3_toosmall; 12 end if; 13 dbms_output.put_line('hello'); 14 exception when t3_toolarge then 15 dbms_output.put_line('too large'); 16 when t3_toosmall then 17 dbms_output.put_line('too small'); 18 end p_test2; 19 /过程已创建。SQL> set serveroutput on; SQL> exec p_test2('a','b',102); too largePL/SQL 过程已成功完成。SQL>
to bzszp(www.bzszp.533.net) : 谢谢你,但为什么case when 不能在这里用呢?书上说case when 是9i才加入的新语法,但我在817版本上的plsql中执行成功,为什么在 过程中不能用?而且用动态sql也不行?
astmpsql varchar2(1000);
t3_toolarge exception;
t3_toosmall exception;
begintmpsql:='
case
when t3>100 then raise t3_toolarge;
when t3<50 then raise t3_toosmall;
end case;
';execute immediate tmpsql;--tmpsql:=''s'';
--execute immediate tmpsql;
dbms_output.put_line('hello');
exception
when t3_toolarge then
dbms_output.put_line('too large');
when t3_toosmall then
dbms_output.put_line('too small');end p_test2;
case
when t3>100 then raise t3_toolarge;
when t3<50 then raise t3_toosmall;
end case;
';execute immediate tmpsql; --?!
我写此段过程的目的是为了试用一下过程中的错误处理以及case是否必须写成动态sql。
t3>100为过大,t3<50为过小。兄台你看能否给写个例子看看,我对oracle很陌生。我的oracle版本为817。
2 as
3 tmpsql varchar2(1000);
4 t3_toolarge exception;
5 t3_toosmall exception;
6 begin
7 if t3>100 then
8 raise t3_toolarge;
9 end if;
10 if t3<50 then
11 raise t3_toosmall;
12 end if;
13 dbms_output.put_line('hello');
14 exception when t3_toolarge then
15 dbms_output.put_line('too large');
16 when t3_toosmall then
17 dbms_output.put_line('too small');
18 end p_test2;
19 /过程已创建。SQL> set serveroutput on;
SQL> exec p_test2('a','b',102);
too largePL/SQL 过程已成功完成。SQL>
谢谢你,但为什么case when 不能在这里用呢?书上说case when 是9i才加入的新语法,但我在817版本上的plsql中执行成功,为什么在
过程中不能用?而且用动态sql也不行?
再请教一个问题,所谓动态sql语句是不是只能生成单条的sql语句,而不能动态生成
pl/sql?
2 as
3 v_str varchar2(200);
4 v_out varchar2(20);
5 v_in varchar2(20);
6 begin
7 v_str := '
8 declare
9 v_dt varchar2(20);
10 begin
11 select to_char(sysdate,''yyyymmdd'') into v_dt from dual;
12 dbms_output.put_line(v_dt);
13 end;';
14 execute immediate v_str;
15 end p_test;
16 /过程已创建。SQL> exec p_test;
20031231PL/SQL 过程已成功完成。
-------------------------------------------
execute immediate ...执行查询语句时,必须保证返回记录
为单行,如果返回多行,要用游标.
书上说oracle9i开始支持case,并且我验证成功。
在oracle817版本中对于case我也验证成功。