当前位置:首页 » 编程语言 » oraclesqlcursor用法
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

oraclesqlcursor用法

发布时间: 2022-07-24 01:49:02

A. Oracle中的游标和cursor是什么,怎么用的啊

1、plsql是面向过程的语言,这类语言还有c,cobol等,这类语言的共同点是一次只能处理一条数据,而数据库sql返回的对象是一个集合,这样直接用plsql程序操作就会出现问题。
2、在这种环境下就出现了游标,游标实际是一个内存地址,只想的是sql查询出的结果集,当需要的时候再根据游标一条一条取数据【fetch】,直到全部数据取完。
---
以上,希望对你有所帮助。

B. ORACLE中用for in 使用cursor

CURSOR cur IS是定义一个游标,然后把游标里定义的查询语句存储到游标里
因为查询语句查出来的数据往往是几条记录
但是你用的时候缺只能一条一条取出来用
这时游标的好处就体现出来了
游标存储时 存的是几条记录
但是读取时 他是一条记录一条记录读取的
然后再使用FOR IN循环一下
就可以将你存储在游标里的记录一条一条的读取出来 但是每次只读取一条
以方便你的使用
这种情况如果不使用游标 往往会出现返回多行结果的错误

C. 游标是什么ORACLE是怎样使用游标的举例说明!

一 游标是什么

游标字面理解就是游动的光标。

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

二 游标的分类

显式游标和隐式游标

显式游标的使用需要4步:

1. 声明游标

CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no = vartype;

2. 打开游标

open mycur(000627)

注:000627是参数

3. 读取数据

fetch mycur into varno, varprice;

4. 关闭游标

close mycur;

三 游标的属性

oracle 游标有4个属性:%ISOPEN,%FOUND,%NOTFOUND,%ROWCOUNT。

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

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

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

四 示例

set serveroutput on;
declare
varno varchar2(20);
varprice varchar2(20);

CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no = vartype;
begin

if mycur%isopen = false then
open mycur(000627);
end if;

fetch mycur into varno,varprice;
while mycur%found
loop
dbms_output.put_line(varno||','||varprice);
if mycur%rowcount=2 then
exit;
end if;
fetch mycur into varno,varprice;
end loop;

close mycur;
end;

PL/SQL记录的结构和C语言中的结构体类似,是由一组数据项构成的逻辑单元。

PL/SQL记录并不保存在数据库中,它与变量一样,保存在内存空间中,在使用记录时候,要首先定义记录结构,然后声明记录变量。可以把PL/SQL记录看作是一个用户自定义的数据类型。

set serveroutput on;
declare

type person is record
(
empno cus_emp_basic.emp_no%type,
empzc cus_emp_basic.emp_zc%type);

person1 person;

cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;

begin
if mycur%isopen = false then
open mycur(000627);
end if;

loop
fetch mycur into person1;
exit when mycur%notfound;
dbms_output.put_line('雇员编号:'||person1.empno||',地址:'||person1.empzc);
end loop;

close mycur;
end;

典型游标for 循环

游标for循环示显示游标的一种快捷使用方式,它使用for循环依次读取结果集中的行数据,当form循环开始时,游标自动打开(不需要open),每循环一次系统自动读取游标当前行的数据(不需要fetch),当退出for循环时,游标被自动关闭(不需要使用close)。使用游标for循环的时候不能使用open语句,fetch语句和close语句,否则会产生错误。

set serveroutput on;
declare

cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;

begin
for person in mycur(000627) loop
dbms_output.put_line('雇员编号:'||person.emp_no||',地址:'||person.emp_zc);
end loop;
end;

D. Oracle中的游标和cursor是什么,怎么用的啊

cursor就是游标的意思
游标一般多用在存储过程当中,用来查询、修改或者删除某些符合条件的数据
详细参见pl/sql编程的游标使用

E. oracle触发器select into和cursor用法的区别

楼主您好

cursor多用于定义遍历一个结果集之前的查询。
然后用fetch into或是for循环遍历(loop)此游标
select into多用于查询出单个值(不是绝对的 比如有bluk collect into)并给自己自定义的变量赋值。这俩其实没太大关系,一般对比cursor和ref cursor,select into 和 :=赋值的区别

F. oracle plsql 练习 要用到游标

唉,又是大学生吧,自己不动脑筋.发个例子你看看.
declare
v_num number := &input;
v_last_num number := 0;
v_result varchar(2000) := '';
begin
loop
v_last_num := mod(v_num,10);
v_result := v_result || to_char(v_last_num);
v_num := v_num - v_last_num;
exit when v_num=0;
v_num := v_num / 10;
end loop;
dbms_output.put_line(v_result);
end;

第六章 游标(cursor)管理
create SYNONYM emp for scott.emp;
--作用:
declare
v_ename emp.ename%type;
begin
select ename into v_ename
from emp;

dbms_output.put_line(v_ename);
end;

--显式游标的定义和遍历
--通过无条件的循环来遍历游标
declare
--1.定义游标
cursor c_emp_cursor
is select ename from emp;

v_ename emp.ename%type;
begin
--2.打开游标
open c_emp_cursor;

--3.遍历游标
loop
fetch c_emp_cursor into v_ename; --rs.next();

exit when c_emp_cursor%NOTFOUND;

dbms_output.put_line(v_ename);
end loop;

--4.关闭游标
close c_emp_cursor;
end;

--用WHILE循环来遍历游标
declare
cursor c_emp_cursor
is select * from emp;

v_row emp%rowtype;
begin
open c_emp_cursor;

fetch c_emp_cursor into v_row;

while c_emp_cursor%FOUND loop
dbms_output.put_line(v_row.ename || ' ' || v_row.empno);
fetch c_emp_cursor into v_row;
end loop;

close c_emp_cursor;
end;

--FOR循环遍历游标的方式
declare
cursor c_emp_cursor
is select * from emp;
begin
for v_row IN c_emp_cursor loop
dbms_output.put_line(v_row.empno || ' ' || v_row.ename);
end loop;
end;

--定义参数类型的游标
declare
cursor c_emp_cursor(p_deptno emp.deptno%type)
is select ename from emp where deptno=p_deptno;

v_ename emp.ename%type;
begin
open c_emp_cursor(&input);

loop
fetch c_emp_cursor into v_ename;
exit when c_emp_cursor%NOTFOUND;
dbms_output.put_line(v_ename);
end loop;

close c_emp_cursor;
end;

--为游标指定返回类型1-rowtype
declare
cursor c_emp_cursor
return emp%rowtype --强制类型
is select * from emp;
begin
for v_row IN c_emp_cursor loop
dbms_output.put_line(v_row.empno);
end loop;
end;

--为游标指定返回类型2-记录(Record)类型
declare
--定义一个记录类型
type t_emp_record is record
(
empno emp.empno%type,
ename emp.ename%type
);

v_emp_record t_emp_record;

cursor c_emp_cursor
return v_emp_record --强制类型
is select empno,ename from emp;
begin
open c_emp_cursor;
loop
fetch c_emp_cursor into v_emp_record;
exit when c_emp_cursor%NOTFOUND;
dbms_output.put_line(v_emp_record.empno);
dbms_output.put_line(v_emp_record.ename);
end loop;
close c_emp_cursor;
end;

--如何通过游标来修改数据
declare
--通过游标修改数据要上行级锁
cursor c_emp_cursor
is select * from emp where deptno=20 for update;
begin
for v_row In c_emp_cursor loop
delete from emp
where current of c_emp_cursor;
end loop;
commit;
end;
select * from emp;

--REF引用类型的游标描述
declare
type t_ref_cursor is ref cursor;

v_ref_cursor t_ref_cursor;
begin
open v_ref_cursor
for select * from emp;

close v_ref_cursor;

open v_ref_cursor
for select * from dept;

close v_ref_cursor;
end;

--1.隐式游标(SQL)的使用
--用途:因为数据库系统在做insert,delete,update,select这些操作的时候
--都是通过游标去完成的,所以,在做操作之前我们的数据库系统都会隐式
--的打开一个游标,在执行操作完毕之后隐式的关闭游标,那么每次的操作的结果
--都会保存在一个叫做SQL的隐式游标中,我们可以通过操作这个隐式游标来获取
--数据库操作的信息

begin
delete from emp where deptno=10;

dbms_output.put_line(SQL%ROWCOUNT);
end;

第七章 存储过程(Procere)和包(Package)

SQLServer - 存储过程 (output参数 return)
Oracle -存储过程(out参数) 函数(return)

--1.存储过程
create or replace procere print(msg varchar2)
is
begin
dbms_output.put_line(msg);
end;

--1.1 调用存储过程
execute print('helloworld');

--1.2
create or replace procere findEmp(p_empno emp.empno%type)
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp where empno=p_empno;

print(v_ename);
exception
when no_data_found then
print('未发现指定员工.');
end;

--1.3 传递参数
create or replace procere myabs(p_num1 IN number,p_num2 OUT number)
is
begin
if p_num1 >0 then
p_num2 := p_num1;
else
p_num2 := (0 - p_num1);
end if;
end;

--调用
declare
v_result number;
begin
myabs(-987,v_result);
print(v_result);
end;

create or replace procere change_num(p_num1 IN OUT number,p_num2 IN OUT number)
is
v_temp number;
begin
v_temp := p_num1;
p_num1 := p_num2;
p_num2 := v_temp;
end;

--调用
declare
v_num1 number :=111;
v_num2 number :=222;
begin
change_num(v_num1,v_num2);

print(v_num1);
print(v_num2);
end;

--2.函数(FUNCTION)
create or replace function myabs1(p_num1 number)
return number
is
begin
if p_num1 >= 0 then
return p_num1;
else
return (0 - p_num1);
end if;
end;

begin
print(myabs1(-999));
end;

create or replace function findEmp1(p_empno emp.empno%type)
return emp.ename%type
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where empno=p_empno;

return v_ename;
exception
when no_data_found then
print('未发现员工');
end;

begin
print(findEmp1(7499));
end;

--3.包(Package)-包(package-定义)和包体(package body-实现)
create or replace package mypkg
is
procere print(msg varchar2);

function findEmp(p_empno number)
return emp.ename%type;
end;

create or replace package body mypkg
is
procere print(msg varchar2)
is
begin
dbms_output.put_line(msg);
end;

function findEmp(p_empno number)
return emp.ename%type
is
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where empno=p_empno;

return v_ename;
exception
when no_data_found then
print('未发现员工');
end;
end;

--4.通过函数返回结果集
create or replace function findAllEmps
return SYS_REFCURSOR
is
v_ref_cursor SYS_REFCURSOR;
begin
open v_ref_cursor
for 'select * from emp';

return v_ref_cursor;
end;
我上学时候练习用的

G. Oracle关于游标的使用,要求最后返回值是游标


create or replace package 名字 as type ReturnCursor is ref Cursor;
procere demo1(Return_Cursor out ReturnCursor);
end 名字;


create or replace package body 名字 as
procere demo1(Return_Cursor out ReturnCursor)
is
strsql varchar2(8000);
begin
strsql :='select a,b from table1';
open Return_Cursor for strsql;
end 名字;

H. oracle plsql cursor的疑问

for update是锁定这些记录,不让其他人更新;不加for update,你在游标读取的时间内别人有可能会更新数据导致不一致

没有for delete和for insert