ORACLE中帶引數 REF游標及動態SQL例項

2021-05-26 20:20:12 字數 2673 閱讀 2261

--***************帶

引數的游標***************===--

declare

dept_code emp.deptno%type; --宣告列型別變數三個

emp_code emp.empno%type;

emp_name emp.ename%type;

cursor emp_cur(deptparam number) is

select empno, ename from emp where deptno = deptparam; --宣告顯示

游標begin

dept_code := &部門編號; --請使用者輸入想檢視的部門編號

open emp_cur(dept_code); --開啟

游標loop

--死迴圈

fetch emp_cur

into emp_code, emp_name; --提取游標值賦給上面宣告的變數

exit when emp_cur%notfound; --如果

游標裡沒有資料則退出迴圈

dbms_output.put_line(emp_code || '' || emp_name); --輸出查詢

end loop;

close emp_cur; --關閉

游標end;

--***************==ref

游標***************===--

accept tab frompt '你想檢視什麼資訊?員工(e)或部門資訊(d):'; --使用accept命令彈出對話方塊讓使用者輸入資料

declare

type refcur_t is ref cursor; --宣告ref

游標型別

refcur refcur_t; --宣告ref

游標型別的變數

pid number;

p_name varchar2(100);

selection varchar2(1) := upper(substr('&tab', 1, 1)); --擷取使用者輸入的字串並轉換為大寫

begin

if selection = 'e' then

--如果輸入的是'e',則開啟refcurr

游標,並將員工表查詢出來賦值給此

游標open refcur for

select empno id, ename name from emp;

dbms_output.put_line('*****員工資訊*****');

elsif selection = 'd' then

--如果輸入是'd',則開啟部門表

open refcur for

select deptno id, dname name from dept;

dbms_output.put_line('*****部門資訊*****=');

else

--否則返回結束

dbms_output.put_line('請輸入員工資訊(e)或部門資訊(d)');

return;

end if;

fetch refcur

into pid, p_name; --提取行

while refcur%found loop

dbms_output.put_line('#' || pid || ':' || p_name);

fetch refcur

into pid, p_name;

end loop;

close refcur; --關閉

游標end;

--***************====動態sql***************==--

variable maxsal number; --宣告變數

execute :maxsal := 2500; --執行引用並給變數賦值

declare

r_emp emp%rowtype; --宣告乙個行型別變數

type c_type is ref cursor; --宣告ref

游標型別

cur c_type; --宣告ref

游標型別的變數

p_salary number; --宣告乙個標量變數

begin

p_salary := :maxsal; --引用變數

--使用using語句將引用到的值傳給動態sql語句'sal >: 1'中的'1'

open cur for 'select * from emp where sal >: 1 order by sal desc'

using p_salary;

dbms_output.put_line('薪水大於' || p_salary || '的員工有:');

loop

fetch cur

into r_emp;

exit when cur%notfound;

dbms_output.put_line('編號:' || r_emp.empno || '姓名:' || r_emp.ename ||

'薪水:' || r_emp.sal);

end loop;

close cur; --關閉

游標end;

oracle帶引數的游標

oracle中的游標可以帶引數麼?具體 怎麼實現呢?可以啊,引數在游標定義時使用,開啟時傳入引數,例如 create or replace procedure a as cursor b c id int is select from d where id c id begin open b 111...

帶引數的游標函式一

create or replacefunction fun odr autoorgschmid odrorgid in varchar2,odrdlvrid in varchar2,odritemid in varchar2,odrorgtype in varchar2 return varchar...

oracle帶游標的儲存過程

create or replace procedure xs test add19 is bachelor edu varchar2 2000 new bachelor edu varchar2 2000 aa varchar2 2000 bb varchar2 2000 edu length in...