PL SQL 例項總結

2021-07-24 04:25:38 字數 4547 閱讀 1010

--pl/sql 保證輸出

set serveroutput on

--pl/sql 塊

declare

v_sal emp.sal%type;

begin

select sal into v_sal from emp where empno=7369;

dbms_output.put_line(v_sal);

end;

--pl/sql 記錄型別

declare

type emp_record is record(

v_ename emp.ename%type,

v_sal emp.sal%type

); v_emp emp_record;

begin

select ename , sal into v_emp from emp where empno=7369;

dbms_output.put_line(v_emp.v_ename||' , '||v_emp.v_sal);

end;

--pl/sql 流程控制

declare

v_sal emp.sal%type;

v_temp varchar2(32);

begin

select sal into v_sal from emp where empno=7369;

if v_sal >=3000 then v_temp := 'sal>=3000';

elsif v_sal >=1000 then v_temp :='1000<=sal<3000';

else v_temp :='sal<1000';

end if;

dbms_output.put_line(v_temp);

end;

--pl/sql 游標

declare

cursor emp_cursor is select ename , sal from emp where deptno=10;

begin

for c in emp_cursor loop

dbms_output.put_line(c.ename||' , '||c.sal);

end loop;

end;

declare

cursor emp_cursor is select empno , sal from emp where deptno=10;

v_temp number(4,2);

begin

for c in emp_cursor loop

if c.sal >=3000 then v_temp := 0.03;

elsif c.sal >=1000 then v_temp :=0.02;

else v_temp :=0.01;

end if;

update emp set sal = c.sal * (1+v_temp) where empno =c.empno;

end loop;

end;

--pl/sql 隱式游標

begin

update emp set sal = sal+10 where empno='7882';

if sql%notfound then dbms_output.put_line('查無此人');

end if;

end;

--pl/sql 異常

declare

v_sal emp.sal%type;

begin

select sal into v_sal from emp where deptno=10;

dbms_output.put_line(v_sal);

exception

when too_many_rows then dbms_output.put_line('返回多行!');

end;

--pl/sql 儲存函式

create or replace function function_helloword(v_name varchar2)

return varchar2

isbegin

return 'hello '||v_name;

end;

begin

dbms_output.put_line(function_helloword(' word!'));

end;

----------

create or replace function function_emp_total(v_deptno number,v_sumemp out number)

return number

is v_sumsal number := 0;

cursor emp_cursor is select sal from emp where deptno = v_deptno;

begin

v_sumemp := 0;

for c in emp_cursor loop

v_sumsal := v_sumsal+c.sal;

v_sumemp :=v_sumemp+1;

end loop;

return v_sumsal;

end;

declare

v_sumemp number := 0;

begin

dbms_output.put_line(function_emp_total(10,v_sumemp));

dbms_output.put_line(v_sumemp);

end;

--pl/sql 儲存過程

create or replace procedure procedure_emp_total(v_deptno number,v_sumsal out number)

is cursor emp_cursor is select sal from emp where deptno = v_deptno;

begin

v_sumsal := 0;

for c in emp_cursor loop

v_sumsal := v_sumsal+c.sal;

end loop;

dbms_output.put_line(v_sumsal);

end;

declare

v_sumsal number := 0;

begin

procedure_emp_total(10,v_sumsal);

end;

--pl/sql 觸發器

create or replace trigger trigger_updateemp

after

update on emp

for each row

begin

dbms_output.put_line('helloword');

end;

update emp set sal =sal+100 where deptno=10;

--游標變數

create or replace procedure createreport(a_checkdate in varchar2) is

v_reportkey varchar2(128) ; --報告編號

v_udsi varchar2(4) ;

--游標變數

type ref_udsi is ref cursor;

cursor_udsi ref_udsi;

--定義游標

cursor cursor_party is

select distinct a.party_id

from pr11_party_udsi_trans a

where a.create_date = to_date(a_checkdate,'yyyy-mm-dd')

and a.udsi != '1102' ;

begin

for p in cursor_party loop

v_reportkey := 'b'||trim(to_char(pr11_report_seq.nextval,'00000000')); --報告編號

--主體觸發的規則

open cursor_udsi for

select a.udsi

from pr11_party_udsi_trans a

where a.create_date = to_date(a_checkdate,'yyyy-mm-dd')

and a.udsi != '1102'

and a.party_id = p.party_id ;

loop

fetch cursor_udsi into v_udsi;

exit when cursor_udsi%notfound;

dbms_output.put_line(v_udsi);

end loop;

end loop;

end;

PL SQL例項講解(一)

題目要求 某加工廠發貨規則是 每車發貨480件,週六週日不發貨,供貨期間除了最後一批貨外其餘只能整車發貨。舉個例子 某廠商需求三天貨,周四需要供貨300,周五需要供貨200,週六需要供貨400,下周一需要供貨200。原發貨表如下 ship date ship qty plan type 2016 1...

pl sql 例項解析 01

1.合併 firstname,lastname.1 declare2 v first name varchar2 255 3 v last name varchar2 255 4 begin5 select first name,last name6 into v first name,v last...

PLSQL集合總結

一 索引表 描述1 索引表只能作為pl sql符合資料型別使用,而不能作為表列的資料型別使用。2 索引表的下標可以為負值,索引表的下標沒有限制。語法type index table type is table of element type index by varchar2 binary inte...