今天一部门开发人员通过公司BQQ问我是否可以在脚本中获取到update语句执行后的更新行数,并想知道返回值。
我做了一个小试验,如下的内容很适合他借以利用。这个案例的关键字有sql%notfound、sql%rowcount和return,适合在脚本或应用中加以使用。
SQL> create table test_return
2 (
3 name varchar2(24)
4 );
Table created
SQL> insert into test_return values('111');
1 row inserted
SQL> insert into test_return values('222');
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> set serveroutput on
--用SQL %NOTFOUND来获取是否更新了行数,true/false
SQL> begin
2 update test_return set name=name where name='123';
3 if SQL %NOTFOUND then
4 dbms_output.put_line('没有数据行更新。');
5 end if;
6 end;
7 /
没有数据行更新。
PL/SQL procedure successfully completed
SQL>
SQL> set serveroutput on
SQL> begin
2 update test_return set name=name where name='111';
3 if SQL %NOTFOUND then
4 dbms_output.put_line('没有数据行更新。');
5 end if;
6 end;
7 /
PL/SQL procedure successfully completed
SQL> commit;
Commit complete
SQL>
--用sql%rowcount来获取更新的行数
SQL> begin
2 update test_return set name=name;
3 dbms_output.put_line('已经更新的行数为:'||to_char(sql%rowcount));
4 end;
5 /
已经更新的行数为:2
PL/SQL procedure successfully completed
SQL> commit;
Commit complete
--如下两个例子获取更新后或插入后的列值
SQL>
SQL> declare
2 v_name varchar2(24);
3 begin
4 update test_return set name=name where rownum<2 returning name into v_name;
5 dbms_output.put_line('已经更新的name为:'||v_name);
6 end;
7 /
已经更新的name为:111
PL/SQL procedure successfully completed
SQL>
SQL> declare
2 v_name varchar2(24);
3 begin
4 insert into test_return(name) values('333') returning name into v_name;
5 dbms_output.put_line('已经更新的name为:'||v_name);
6 end;
7 /
已经更新的name为:333
PL/SQL procedure successfully completed
SQL> commit;
Commit complete
SQL>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/38542/viewspace-984943/ ,如需转载,请注明出处,否则将追究法律责任。