时间:2021-07-01 10:21:17 帮助过:12人阅读
create
or
replace
procedure t_p(l_in in
out
number)
is
begin
l_in := 5;
end;
@Test
public void test() throws SQLException
{
DataSource ds = DataSourceGen.getDataSourceFromXML();
Connection conn = ds.getConnection();
int inValue = 0;
CallableStatement cs = conn.prepareCall("{call t_p(?)}");//注意有大括号
cs.setInt(1, inValue);//设置传入的值 下标从1开始
cs.registerOutParameter(1, Types.INTEGER);//注册传出的值
cs.executeUpdate();//执行
int outValue = cs.getInt(1);//获取输出
System.out.println(inValue);//输出0
System.out.println(outValue);//输出5
conn.close();
}
create
or
replace
function t_f(l_in in
out
number)
return
number
is
l_c number;
begin
l_in := 5;
l_c := 2;
return l_c;
end;
@Test
public void test() throws SQLException
{
DataSource ds = DataSourceGen.getDataSourceFromXML();
Connection conn = ds.getConnection();
int inValue = 0;
CallableStatement cs = conn.prepareCall("{? = call t_f(?)}");
cs.setInt(2, inValue);//设置传入的值
cs.registerOutParameter(1, Types.INTEGER);//注册传出的值-通过return返回值
cs.registerOutParameter(2, Types.INTEGER);//注册传出的值-通过参数返回值
cs.executeUpdate();
int outValue1 = cs.getInt(1);
int outValue2 = cs.getInt(2);
System.out.println(inValue);//输出0
System.out.println(outValue1);//输出2
System.out.println(outValue2);//输出5
conn.close();
}
也可以通过select t_f(1) from dual;调用,但是具有out参数的函数不可以通过sql调用。
g_globle number;
procedure set_globle(number_in in number);
end scope_demo;
create or replace package body scope_demo is
procedure set_globle(number_in in number) is
l_count pls_integer;
l_name varchar2(10) := ‘北京‘;
begin
<<localscope>>--标签 为匿名块命名
declare
l_use char(1) := ‘1‘;
begin
select count(1)
into set_globle.l_count
from pub_organ o
where o.organ_name like set_globle.l_name || ‘%‘
and o.in_use = localscope.l_use;
dbms_output.put_line(set_globle.l_count);
end localscope;
scope_demo.g_globle := set_globle.number_in;
end set_globle;
end scope_demo;
begin
scope_demo.set_globle(2);
dbms_output.put_line(scope_demo.g_globle);
end;
procedure local_precedure is
begin
dbms_output.put_line(‘dd‘);
end local_precedure;
begin
for i in 1..10
loop
local_precedure();
end loop;
end;
l_c char(1);
begin
l_c := ‘‘;
if (l_c is null) then
dbms_output.put_line(1);
elsif (l_c = ‘ ‘) then
dbms_output.put_line(2);--输出2 因为自动填满了 而在sql中不会自动填满
end if;
end;
l_n number := ‘10‘;
begin
if(l_n in (‘10‘,‘20‘))
then dbms_output.put_line(‘in‘);
end if;
if(l_n like ‘10%‘)
then dbms_output.put_line(‘like‘);
end if;
end;
l_c pls_integer;
begin
l_c := 0;
loop
--exit when l_c > 10;
if (l_c > 10) then
exit;
end if;
dbms_output.put_line(l_c);
l_c := l_c + 1;
end loop;
end;
declare
l_c pls_integer;
begin
l_c := 0;
while l_c <= 10 loop —while的条件可以加括号 也可以不加
dbms_output.put_line(l_c);
l_c := l_c + 1;
end loop;
end;
只有数值型for循环和游标型(可以是游标,也可以直接是sql查询)for循环,这两种循环都要有in字句。遍历的变量不用声明,系统会自动生成。
begin
for l_c in 0 .. 10 loop –不能加括号
dbms_output.put_line(l_c);
end loop;
end;
begin
for l_organ in (select * from pub_organ o where o.organ_name like ‘北京%‘) loop –查询必须用括号括起来
dbms_output.put_line(l_organ.organ_name);
end loop;
end ;
i pls_integer := 0;
j pls_integer := 0;
begin
<<outerloop>>
loop
dbms_output.put_line(i);
exit when i > 10;
j := 0;
<<innerloop>>
loop
dbms_output.put_line(‘ ‘ || j);
exit when j > 10;
exit outerloop when(j = 5 and i = 5);
j := j + 1;
end loop innerloop;
i := i + 1;
end loop outerloop;
end;
when others then
null;
3、
create
or
replace
procedure t_pp as
l_n number;
begin
update pub_organ o set o.organ_name = ‘dddd444ddd‘ where o.organ_id = ‘O50649821‘;
l_n := ‘ddd‘;--此处出错 本层未捕捉 自动抛出异常到上层
end;
begin
t_pp();--此处会捕捉到下层抛出的异常
exception
when others then
rollback;--回滚
commit;
end;
l_ex1 exception;
l_ex2 exception;
begin
begin
raise l_ex1;
end;
exception
when l_ex2
then dbms_output.put_line(‘l_ex1‘);
end;
l_ex1 exception;
l_ex2 exception;
pragma exception_init(l_ex1,-20111);
pragma exception_init(l_ex2,-20111);
begin
begin
raise l_ex1;
end;
exception
when l_ex2
then dbms_output.put_line(‘l_ex1‘);--此处将成功打印
end;
l_organ_id varchar2(32);
l_organ_name varchar2(500);
begin
l_organ_id := ‘222‘;
select o.organ_name into l_organ_name from pub_organ o where o.organ_id = l_organ_id;
end;
declare
l_organ_id varchar2(32);
l_organ_name varchar2(500);
err_no_organ_1403 exception;
pragma exception_init(err_no_organ_1403, -1403);
begin
l_organ_id := ‘222‘;
select o.organ_name
into l_organ_name
from pub_organ o
where o.organ_id = l_organ_id