oracle plsql 練習 要用到游標

2021-08-31 04:27:08 字數 1959 閱讀 5561

1、編寫儲存過程,顯示各個職位(job),工資在2000元分以上人數和1000元以下人數。

2、編寫乙個儲存過程

將各個職位前3位的職員的姓名,職位,工資,名次按工資排序存到乙個資料表good中

解答:1、

create or replace procedure test_cursor

iscursor c_tmp1 is select '>=2000' as salway,job,sum(case when sal>=2000 then 1 else 0 end) as countnum from emp group by job ;

cursor c_tmp2 is select '<=1000' as salway,job,sum(case when sal<=1000 then 1 else 0 end) as countnum from emp group by job ;

begin

for r_tmp1 in c_tmp1

loop

dbms_output.put_line('職位'||r_tmp1.job||'大於2000元的人數為'||r_tmp1.countnum);

end loop;

dbms_output.put_line(chr(13)); -- 列印回車

for r_tmp2 in c_tmp2

loop

dbms_output.put_line('職位'||r_tmp2.job||'小於1000元的人數為'||r_tmp2.countnum);

end loop;

end test_cursor;

2、建表:

drop table good;

create table good(

gid number(5) primary key,

gename varchar2(20) not null,

gejob varchar2(20) not null,

gesal number not null,

grank integer not null

) ;-- 建序列

create sequence s_good

minvalue 1

maxvalue 99999999999999999999999999

start with 1

increment by 1

cache 20;

-- 建觸發器

create trigger t_good before insert on good for each row

begin

select s_good.nextval into :new.gid from dual;

end;

--建過程

create or replace procedure test_cursor2

iscursor c_tmp is select * from good;

begin

insert into good(gename,gejob,gesal,grank) select * from (select ename gename,job gejob,sal gesal,row_number() over(partition by job order by sal) grank from emp) where grank<4;

commit;

for r_tmp in c_tmp

loop

dbms_output.put_line('姓名:'||r_tmp.gename||',職位:'||r_tmp.gejob||',薪水:'||r_tmp.gesal||',職位排名:'||r_tmp.grank);

end loop;

end test_cursor2;

測試:begin

--test_cursor;

test_cursor2;

end;

OraclePL SQL儲存過程

create or replace 建立或替換,如果存在就替換,不存在就建立create or replace procedure piscursor cisselect from dept2 for update beginfor row record in c loopif row record...

Oracle pl sql基礎 迴圈

一 迴圈語法以及用法 1 loop的語法以及用法,如下 loop 處理程式 end loop 例子 declare v counter binary integer 1 定義變數 begin loop dbms output.put line v counter v counter v counte...

Oracle PL SQL異常處理

case語句語法格式如下 case 變數 when 表示式1 then 值1 when 表示式2 then 值2 when 表示式n then 值n else 值n 1 end 1 使用case語句寫乙個pl sql塊,要求輸入員工編號,根據員工的職位進行工資提公升,提公升要求如下 如果職位是cle...