oracle存儲過程使用游標
㈠ oracle存儲過程游標問題
fetch mycur into yang_02;
把游標mycur 所提取的數據yang_02變數
㈡ oracle存儲過程 游標和動態賦值問題
創建:
create or replace procere t_ts(ref_cur out sys_refcursor) AS
BEGIN
open ref_cur for SELECT leixing FROM diaobodan;
end t_ts;
調用:
declare
s_cur SYS_REFCURSOR;
v_leixing varchar2(100);
begin
t_ts(s_cur);
loop
fetch s_cur into vv_name;
exit when s_cur%notfound;
dbms_output.put_line(v_leixing);
end loop;
end;
是這個意思嗎?
㈢ Oracle存儲過程游標for循環怎麼寫
一、不帶參數的游標for循環
1
首先編寫存儲過程的整體結構,如下:
create or replace procere test_proc is
v_date date; --變數定義
begin
select sysdate into v_date from al;
end test_proc;
2
定義游標:
create or replace procere test_proc is
v_date date; --定義變數
cursor cur is select * from ldcode; --定義游標
begin
select sysdate into v_date from al;
end test_proc;
3
編寫for循環:
create or replace procere test_proc is
v_date date; --定義變數
cursor cur is select * from ldcode where rownum<10; --定義游標
begin
select sysdate into v_date from al;
--游標for循環開始
for temp in cur loop --temp為臨時變數名,自己任意起
Dbms_Output.put_line(temp.Code); --輸出某個欄位,使用"變數名.列名"即可。
end loop;
--游標for循環結束
end test_proc;
4
測試運行,點擊【DBMS Output】標簽頁查看結果如下圖:
END
二、帶參數的游標for循環
1
定義帶參數的游標:
cursor cur(v_codetype ldcode.Codetype%TYPE) is
select * from ldcode where codetype = v_codetype; --定義游標
定義游標格式:
cursor 游標名稱(變數定義) is 查詢語句;
注意:
where條件中的變數名v_codetype要與游標定義cur(v_codetype ldcode.Codetype%TYPE)中的一致。
2
編寫for循環部分:
--游標for循環開始
for temp in cur('llmedfeetype') loop
--temp為臨時變數名,自己任意起
--cur('llmedfeetype')為"游標名稱(傳入的變數)"
Dbms_Output.put_line(temp.Code); --輸出某個欄位,使用"變數名.列名"即可。
end loop;
--游標for循環結束
3
測試運行,點擊【DBMS Output】標簽頁查看結果如下圖:
http://jingyan..com/article/67508eb437e39e9ccb1ce46a.html
㈣ oracle存儲過程游標使用疑問
1、for t_name in (select ...) loop
這個是隱式游標,相當於一個結果集,隱式Cursor由系統自動打開和關閉。
exit when %notfound是配合fetch使用,沒有fetch就不需要。
你第一個存儲過程可以這樣寫:
create or replace procere d_1 is
begin
for cur in (select * from t_t) ---這個cur是隱式游標,無需定義,直接使用。
loop
dbms_output.put_line(cur.name);
end loop;
end;
/
2、使用的是標準的顯式游標
a 定義游標---Cursor [Cursor Name] IS;
b 打開游標---Open [Cursor Name];
c 操作數據---Fetch [Cursor name]
d 關閉游標---Close [Cursor Name]
希望能幫到你。
㈤ oracle存儲過程中打開游標有幾種方法用open直接打開
兩種方法
1.聲明游標時寫好SELECT語句,如
CURSOR r_cur1 IS select *** from tableName where 條件;
使用時
OPEN r_cur1;
LOOP
FETCH *** INTO variable;
EXIT WHEN r_cur1%NOTFOUND OR r_cur1%NOTFOUND IS NULL;
。。。
2.聲明游標
ccc sys_refcursor;
使用時
open ccc for select dept_code,dept_name from comm.dept_dict;
loop
fetch ccc into aa,bb;
exit when ccc%notfound;
。。。
end loop;
close ccc;
㈥ oracle存儲過程中使用游標作為out類型參數,求救!
1、你定義的【RET_CURSOR_VALUE】是一個游標變數,當他作為參數傳入過程【GET_EMPINFOBYDEPNO】時候已經被打開(就是open...for),在主程序中再度打開是沒有必要的也是錯誤的(且游標變數只能用【open...for】的形式打開)。
2、所以把【OPEN
RET_CURSOR_VALUE】這句話刪掉,就沒有問題了。
*******************************
口說無憑,oracle實施log請參照:
說明:我把empno改成20了(因為表裡面沒有1的數據),還有把open語句注掉了。
*******************************
[SCOTT@ORA1]
sql>DECLARE
2
RET_CURSOR_VALUE
PKG_CONST.REF_CURSOR;
3
RET_EMPNO
EMP.EMPNO%TYPE;
4
RET_ENAME
EMP.ENAME%TYPE;
5
BEGIN
6
GET_EMPINFOBYDEPNO(20,
RET_CURSOR_VALUE);
7
--OPEN
RET_CURSOR_VALUE
;--報游標類型有誤,未解決!!!
8
LOOP
9
FETCH
RET_CURSOR_VALUE
10
INTO
RET_EMPNO,
RET_ENAME;
11
EXIT
WHEN
RET_CURSOR_VALUE%NOTFOUND;
12
DBMS_OUTPUT.PUT_LINE('empNo
is:'
||
RET_EMPNO
||
',empName
is'
||
13
RET_ENAME);
14
END
LOOP;
15
CLOSE
RET_CURSOR_VALUE;
16
END;
17
/
empNo
is:7369,empName
isSMITH
empNo
is:7566,empName
isJONES
empNo
is:7902,empName
isFORD
---
以上,希望對你有所幫助。
㈦ Oracle存儲過程游標for循環怎麼寫
首先編寫存儲過程的整體結構,如下:
create or replace procere test_proc is
v_date date; --變數定義
begin
select sysdate into v_date from al;
end test_proc;
㈧ orcal的存儲過程 游標的使用
CREATE OR REPLACE PROCEDURE SEND_SMS
(
content IN varchar2,
now_date IN varchar2,
s_org_id IN number,
s_dept_id IN number,
s_user_id IN number,
actor_id IN number
) iS
cursor sss IS select tam.objectid,tam.actortype from ts_actors ta,ts_actormembers tam where ta.actorid=tam.actorid and ta.actorid=actor_id;
cursor ttt(prm_objectid) IS select tu.user_id from ts_station ts,ts_user_station tu where ts.station_id=prm_objectid and ts.station_id=tu.station_id;
temp_mobile varchar2(20);
--temp_objectId varchar2(20);
temp_s_mobile varchar2(20);
begin
for s in sss loop
--temp_objectId:=s.objectid;
if s.actortype=20 then
select u.mobile into temp_mobile from ts_user u where u.userid=s.objectid;
insert into oa_sms values(sys_guid(),temp_mobile,'',content,0,0,now_date,s_org_id,s_dept_id,s_user_id);
end if;
if s.actortype=30 then
for st in ttt(s.objectid) loop
select u.mobile into temp_s_mobile from ts_user u where u.userid=st.user_id;
insert into oa_sms values(sys_guid(),temp_s_mobile,'',content,0,0,now_date,s_org_id,s_dept_id,s_user_id);
end loop;
end if;
end loop;
end;
我修改了一下,由於你過程中的表,我本地沒有,所以沒有檢查,不過應該差不多,第二個游標和變數都在外部聲明,被我注掉的地方沒有什麼用處了。把從外部游標中獲取的值作為參數傳入的第二個游標中就可以了。不用像之前那麼些。如果有問題,可以給我留言。
希望可以幫助你,期待成為最佳答案。O(∩_∩)O~
㈨ oracle在存儲過程中定義游標
createtableemp
(idvarchar2(10),
namevarchar2(20),
sexnumber,
tyvarchar2(20)
);
insertintoempvalues('001','Tom',1,'gcs');
insertintoempvalues('002','John',1,'dba');
insertintoempvalues('003','Jean',0,'gcs');
insertintoempvalues('004','Reid',1,'gcs');
commit;
createorreplaceprocerepro6as
cursorcris
select*
fromemp
wheresex=1
andty='gcs';
begin
forcr_resultincrloop
begin
dbms_output.put_line('ID:'||cr_result.id||'NAME:'||
cr_result.name);
end;
endloop;
endpro6;