Oracle PL/SQL匿名块(三)
轉載:? http://blog.163.com/lyq_163_2009/blog/static/134082696201071210464738/
//錯誤處理
declare?
? v_temp number(4);
begin
? select empno into v_temp from emp where deptno=10;
exception
? when too_many_rows then
? ? dbms_output.put_line('太多記錄了...');
? when others then
? ? dbms_output.put_line('error...');
end;
/
?
?
//錯誤處理
declare
? v_temp number(4);
begin
? select empno into v_temp from emp where empno=2222;
exception
? when no_data_found then
? ? dbms_output.put_line('沒有數據...');
? when others then
? ? dbms_output.put_line('error...');
end;
/
?
?
//游標
declare
? cursor c is select * from emp;
? v_emp c%rowtype;
begin?
? open c;
? fetch c into v_emp;
? dbms_output.put_line(v_emp.ename);
? close c;
end;
/
?
?
//游標
declare
? cursor c is select * from emp;
? v_emp c%rowtype;
begin
? open c;
? loop
? ? fetch c into v_emp;
? ? exit when(c%notfound);
? ? ? dbms_output.put_line(v_emp.ename);
? end loop;
? close c;
end;
/
?
?
//游標
declare
? cursor c is select * from emp;
? v_emp c%rowtype;
begin
? open c;
? fetch c into v_emp;
? while(c%found)loop
? ? dbms_output.put_line(v_emp.ename);
? ? fetch c into v_emp;
? end loop;
? close c;
end;
/
?
?
//游標
declare
? cursor c is select * from emp;
begin
? for v_emp in c loop
? ?dbms_output.put_line(v_emp.ename);
? end loop;
end;
/
?
//帶參數的游標
declare
? cursor c(v_deptno emp.deptno%type, v_job emp.job%type) is?
? ? select ename,sal from emp where deptno=v_deptno and job=v_job;
? --v_temp c%rowtype;
begin
? --open c(30,'clerk');
? for v_temp in c(30,'CLERK') loop
? ? dbms_output.put_line(v_temp.ename || ' ' || v_temp.sal);
? end loop;
end;
/
?
?
//可以更新的游標
declare
? cursor c is select * from emp2 for update;
? --v_temp c%rowtype;
begin?
? for v_temp in c loop
? ? if(v_temp.sal < 2000)then
? ? ? update emp2 set sal=sal*2 where current of c;
? ? elsif(v_temp.sal =5000)then
? ? ? delete from emp2 where current of c;
? ? end if;
? end loop;
? commit;
end;
/
?
//創建表
create table emp2 as(
? ?select * from emp
);
//刪除表
drop table emp2;
轉載于:https://blog.51cto.com/baidu2013/1118699
總結
以上是生活随笔為你收集整理的Oracle PL/SQL匿名块(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx应用案例分享:压力测试
- 下一篇: 数据库中使用自增量字段与Guid字段作主