快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

oracle游标怎么存储 oracle 存储过程调用有游标的存储过程

oracle 存储过程 游标

你游标木有定义把,一般都是这样写的.还有你的列名是a1,前面加了个限定t是把?如果加限定的话,那在from后面的数据来源表,就需要标明那个表是t.

创新互联专业为企业提供清原网站建设、清原做网站、清原网站设计、清原网站制作等企业网站建设、网页设计与制作、清原企业网站模板建站服务,10多年清原做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

vstr1 varchar2(100)

vstr2 varchar2(100)

declare r_cur cursor for select t.a1,t.a2 from abc t

open r_cur

fetch from r_cur into vstr1,vstr2

后面就是

while @@FETCH_STATUS = 0 .....等等不写了。

反正游标头我都是那么写,没有问题。

oracle存储过程游标问题

--打开游标并提取后面SQL的第一行数据放到游标中 这里就是打开游标

open for 是隐式游标的写法 不建议使用 这种游标好象不需要关闭 具体你自己测试下 而且少了expection 处理

fetch mycur into yang_02;

--循环插入数据

多余了 可以不要 前面有fetch了如果还有这里的话 就只能插入奇数行

其他没什么问题了 还有你这个过程用ref cursor是浪费 没必要用 用简单的显示游标更好点

Oracle存储过程和游标操作

-- 第一个题目,我的表叫stu,你别忘了改成你的表名

create or replace procedure pro7

as

cursor c_emp1 is select ename,sal from stu;

vename stu.ename%type;

vsal stu.sal%type;

vnewsal stu.sal%type;

vfd number := 0;

begin

open c_emp1;

loop

fetch c_emp1 into vename,vsal;

exit when c_emp1%notfound;

vfd := vsal*0.2;

if vfd = 300

then update stu set sal = sal+vfd where ename=vename;

vnewsal := vsal+vfd;

dbms_output.put_line('员工' || vename || '涨了' || vnewsal || '工资');

end if;

end loop;

close c_emp1;

end;

oracle数据库的游标和存储过程怎么写?

Oracle游标,从字面理解就是游动的光标。用数据库语言来描述就是:游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了,将游标放置到某行后,即可对该行数据进行操作,例如提取当前行的数据等。

游标分为显示游标和隐式游标。

要使用显示游标分为四步:

1.声名游标。

cursor sel_names is select * from names;

2.打开游标。

open sel_names;

3.读取数据。

fetch sel_name into RowTypeVariable;

4.关闭游标。

close sel_name;

Oracle游标的属性:

1.%ISOPEN判断游标是否被打开,如果打开%ISOPEN等于true,否则等于false;

2.%FOUND判断游标所在的行是否有效,如果有效,则%FOUND等于true,否则等于false;

3.%ROWCOUNT返回当前位置为止游标读取的记录行数。

例子:

set serveroutput on;

declare

temp_name varchar2(32);

cursor sel_name

is

select * from names;

begin

if sel_names%isopen = false then

open sel_names;

end if;

fetch sel_names into temp_name;

while sel_names%found loop

dbms_output.put_line('name: '||temp_name);

if sel_names%rowcount=200 then

exit;

end if;

fetch sel_names into temp_name;

end loop;

close sel_names;

end;

/

Oracle 游标

游标能够根据查询条件从数据表中提取一组记录,将其作为一个临时表置于数据缓冲区中,利用指针逐行对记录数据进行操作。

Oracle中的游标分为显示游标和隐式游标 。

在执行SQL语句时,Oracle会自动创建隐式游标,该游标是内存中处理该语句的数据缓冲区,存储了执行SQL语句的结果。通过隐式游标属性可获知SQL语句的执行状态信息。

%found:布尔型属性,如果sql语句至少影响到一行数据,值为true,否则为false。

%notfound:布尔型属性,与%found相反。

%rowcount:数字型属性,返回受sql影响的行数。

%isopen:布尔型属性,当游标已经打开时返回true,游标关闭时则为false。

用户可以显式定义游标。使用显式游标处理数据要4个步骤:定义游标、打开游标、提取游标数据和关闭游标。

游标由游标名称和游标对应的select结果集组成。定义游标应该放在pl/sql程序块的声明部分。

语法格式:cursor 游标名称(参数) is 查询语句

打开游标时,游标会将符合条件的记录送入数据缓冲区,并将指针指向第一条记录。

语法格式:open 游标名称(参数);

将游标中的当前行数据赋给指定的变量或记录变量。

语法格式:fetch 游标名称 into 变量名;

游标一旦使用完毕,就应将其关闭,释放与游标相关联的资源。

语法格式:close 游标名称;

declare

cursor c1 is  select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

loop

  fetch c1 into v_sno,v_cno,v_grade;

  exit when c1%notfound;--紧跟fetch之后

if c1%found then

dbms_output.put_line(to_char(c1%rowcount)||v_cno);

end if;

end loop;

close c1; 

end;

declare

cursor c1 is select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

fetch c1 into v_sno,v_cno,v_grade;

while c1%found loop

  dbms_output.put_line(v_sno||v_cno||v_grade);

 fetch c1 into v_sno,v_cno,v_grade;

end loop;

close c1; 

end;

第三种:for

declare

cursor c1 is select sno,cno,grade from sc;

begin

for item in c1 loop

dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));

end loop;

end;

Oracle存储过程游标for循环怎么写

一、不带参数的游标for循环

1

首先编写存储过程的整体结构,如下:

create or replace procedure test_proc is

v_date date; --变量定义

begin

select sysdate into v_date from dual;

end test_proc;

2

定义游标:

create or replace procedure test_proc is

v_date date; --定义变量

cursor cur is select * from ldcode; --定义游标

begin

select sysdate into v_date from dual;

end test_proc;

3

编写for循环:

create or replace procedure test_proc is

v_date date; --定义变量

cursor cur is select * from ldcode where rownum10; --定义游标

begin

select sysdate into v_date from dual;

--游标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


新闻标题:oracle游标怎么存储 oracle 存储过程调用有游标的存储过程
文章位置:http://6mz.cn/article/hehshc.html

其他资讯