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;