1. 高手求助。 oracle数据库的带参数游标问题。
cursor pdct-parameter-cur (id VARCHAR,price NUMBER)
IS SELECT * FROM PRODUCTINFO
WHERE PRODUCTID LIKE ID \\ '%'
AND proctprice>price;
声明了一个带参数游标,
WHERE PRODUCTID LIKE ID || '%' 这里是把参数ID实际值后面增加一个“%”通配符,用来like匹配。如果ID传进来值为'123456',那个条件就相当于WHERE PRODUCTID LIKE '123456%' 。
2. oracle sql语句中的游标,函数和存储过程的区别
游标有显示游标和隐式游标之分,显示游标需要自己去定义,declare cursor(这个自己看书去吧),这样使用游标一条条地取值使用。而隐式游标针对所有的select语句都是的。
函数和存储过程几乎没有区别,只是前者有返回值,后者没有,如果后者定义了出参,那就和函数的功能是一样的了。
3. 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;
我上学时候练习用的
4. oracle中游标的使用
这样的问法好笼统,不过,还是把我整理的给你看看吧~个人整理,无复制的哦~
A:分类:
1:隐式游标:非用户明确声明而产生的游标. 你根本看不到cursor这个关键字.
2:显示游标:用户明确通过cursor关键字来声明的游标.
3:引用游标:
B:什么是隐式游标:
1:什么时候产生:
会在执行任何合法的SQL语句中产生.他不一定存放数据.也有可能存放记录集所影响的行数.
如果执行SELECT语句,这个时候游标会存放数据.如果执行INSERT、UPDATE、 DELETE会存放记录影响的行数.
C:隐式游标叫什么名字:
名字叫sql
关于sql的游标变量到底有哪些呢?
作用:返回最近一次执行SQL语句所涉及的游标相关信息.因为每执行一次SQL语句,都会产生一个隐式游标.那么当前执行的SQL语句就是当前的隐式游标.
sql%found
sql%notfound
sql%rowcount
sql%isopen
D:关于隐式游标的例子:
create table 学生基本信息表
(
StuID number(2),
StuName varchar2(20)
)
alter table 学生基本信息表 add constraint PK_STUID primary key(StuID)
declare
num number:=0;
begin
num:=#
delete from 学生基本信息表 where StuID=num;
if sql%notfound then
dbms_output.put_line('该行数据没有发现');
else
dbms_output.put_line('数据被发现并删除,影响的行数为:'||sql%rowcount);
end if;
end;
E:关于显示游标的例子:
1:如何定义显示游标
declare cursor <cursor_name> is [select语句];
declare cursor mycur is select empno,ename,job from scott.emp;
2:如何打开游标:
open <cursor_name>;
open mycur;
3:如何通过游标来读取数据
fetch <cursor_name> into <variable_list>
4:如何关闭游标:
close <cursor_name>;
close mycur;
注意:在Oracle中,不需要显示销毁游标.因为在Oracle中,很多东西是由JAVA写的.Oracle会自动销毁游标.
5:举例:
declare
cursor mycur is select empno,ename,job from emp; --创建游标
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
begin
open mycur;--打开游标
fetch mycur into vempno,vename,vjob;--使用数据
dbms_output.put_line('I Found You!'||mycur%rowcount||'行');
dbms_output.put_line('读取的数据为'||vempno||' '||vename||' '||vjob);
close mycur;
end;
因为只读出来一条,所以要遍历一下:
declare
cursor mycur is select empno,ename,job from emp;
vempno emp.empno%type;
vename emp.ename%type;
vjob emp.job%type;
begin
open mycur;
loop—循环,遍历
fetch mycur into vempno,vename,vjob;
exit when mycur%notfound;
if mycur%found then
dbms_output.put_line('读取的数据为'||vempno||' '||vename||' '||vjob);
end if;
end loop;
dbms_output.put_line('I Found You!'||mycur%rowcount||'行');
close mycur;
end;
6:通常情况下我们在读取表数据的时候,我们需要动态的去查询.所以能不能在Oracle中给游标带参数呢?可以!
1):如何定义带参数的游标:
declare cursor <cursor_name>(参数名称 参数类型描述) is select xxxxx from bbbbb where aaa==??? and ccc=???;
2):例子:
游标是一个集合,读取数据有两种方式
第一种方式: open fetch close
第二种方式: for 一但使用了for循环 ,在循环刚刚开始的时候,相当于执行open,在处理循环的时候,相当于执行fetch,在退出循环的时候,相当于执行了close
Declare cursor query(vname varchar) is select empno,ename,job from emp where ename like'%'||vname||'%';
三种游标的例子:
--
DECLARE
CURSOR TEST_CUR IS
SELECT * FROM EMP;
MY_CUR EMP%ROWTYPE;
BEGIN
OPEN TEST_CUR;
FETCH TEST_CUR INTO MY_CUR;
WHILE TEST_CUR%FOUND
LOOP
DBMS_OUTPUT.PUT_LINE(MY_CUR.EMPNO||','||MY_CUR.ENAME);
FETCH TEST_CUR INTO MY_CUR;
END LOOP;
CLOSE TEST_CUR;
END;
--
DECLARE
CURSOR TEST_CUR IS
SELECT * FROM EMP;
BEGIN
FOR MY_CUR IN TEST_CUR
LOOP
DBMS_OUTPUT.PUT_LINE(MY_CUR.EMPNO||','||MY_CUR.ENAME);
END LOOP;
END;
--
DECLARE
CURSOR TEST_CUR(V_SAL NUMBER) IS
SELECT *
FROM EMP
WHERE SAL > V_SAL;
V_SAL1 NUMBER ;
BEGIN
V_SAL1 := 2000;
FOR MY_CUR IN TEST_CUR(V_SAL1)
LOOP
DBMS_OUTPUT.PUT_LINE(MY_CUR.EMPNO||','||MY_CUR.ENAME);
END LOOP;
END;
5. 关于oracle8i的带参数游标用法的问题
oracle数据库游标使用大全
sql是用于访问oracle数据库的语言,pl/sql扩展和加强了sql的功能,它同时引入了更强的程序逻辑。 pl/sql支持dml命令和sql的事务控制语句。ddl在pl/sql中不被支持,这就意味作在pl/sql程序块中不能创建表或其他任何对象。较好的pl/sql程序设计是在pl/sql块中使用象dbms_sql这样的内建包或执行execute immediate命令建立动态sql来执行ddl命令,pl/sql编译器保证对象引用以及用户的权限。
下面我们将讨论各种用于访问oracle数据库的ddl和tcl语句。
查询
select语句用于从数据库中查询数据,当在pl/sql中使用select语句时,要与into子句一起使用,查询的返回值被赋予into子句中的变量,变量的声明是在delcare中。select into语法如下:
select [distict|all]{*|column[,column,...]}
into (variable[,variable,...] |record)
from {table|(sub-query)}[alias]
where............
pl/sql中select语句只返回一行数据。如果超过一行数据,那么就要使用显式游标(对游标的讨论我们将在后面进行),into子句中要有与select子句中相同列数量的变量。into子句中也可以是记录变量。
%type属性
在pl/sql中可以将变量和常量声明为内建或用户定义的数据类型,以引用一个列名,同时继承他的数据类型和大小。这种动态赋值方法是非常有用的,比如变量引用的列的数据类型和大小改变了,如果使用了%type,那么用户就不必修改代码,否则就必须修改代码。
例:
v_empno scott.emp.empno%type;
v_salary emp.salary%type;
不但列名可以使用%type,而且变量、游标、记录,或声明的常量都可以使用%type。这对于定义相同数据类型的变量非常有用。
delcare
v_a number(5):=10;
v_b v_a%type:=15;
v_c v_a%type;
begin
dbms_output.put_line
(''v_a=''||v_a||''v_b=''||v_b||''v_c=''||v_c);
end
sql>/
v_a=10 v_b=15 v_c=
pl/sql procere successfully completed.
sql>
其他dml语句
其它操作数据的dml语句是:insert、update、delete和lock table,这些语句在pl/sql中的语法与在sql中的语法相同。我们在前面已经讨论过dml语句的使用这里就不再重复了。在dml语句中可以使用任何在declare部分声明的变量,如果是嵌套块,那么要注意变量的作用范围。
例:
create or replace procere fire_employee (pempno in number)
as
v_ename emp.ename%type;
begin
select ename into v_ename
from emp
where empno=p_empno;
insert into former_emp(empno,ename)
values (p_empno,v_ename);
delete from emp
where empno=p_empno;
update former_emp
set date_deleted=sysdate
where empno=p_empno;
exception
when no_data_found then
dbms_output.put_line(''employee number not found!'');
end
dml语句的结果
当执行一条dml语句后,dml语句的结果保存在四个游标属性中,这些属性用于控制程序流程或者了解程序的状态。当运行dml语句时,pl/sql打开一个内建游标并处理结果,游标是维护查询结果的内存中的一个区域,游标在运行dml语句时打开,完成后关闭。隐式游标只使用sql%found,sql%notfound,sql%rowcount三个属性.sql%found,sql%notfound是布尔值,sql%rowcount是整数值。
sql%found和sql%notfound
在执行任何dml语句前sql%found和sql%notfound的值都是null,在执行dml语句后,sql%found的属性值将是:
. true :insert
. true :delete和update,至少有一行被delete或update.
. true :select into至少返回一行
当sql%found为true时,sql%notfound为false。
sql%rowcount
在执行任何dml语句之前,sql%rowcount的值都是null,对于select into语句,如果执行成功,sql%rowcount的值为1,如果没有成功,sql%rowcount的值为0,同时产生一个异常no_data_found.
sql%isopen
sql%isopen是一个布尔值,如果游标打开,则为true, 如果游标关闭,则为false.对于隐式游标而言sql%isopen总是false,这是因为隐式游标在dml语句执行时打开,结束时就立即关闭。
事务控制语句
事务是一个工作的逻辑单元可以包括一个或多个dml语句,事物控制帮助用户保证数据的一致性。如果事务控制逻辑单元中的任何一个dml语句失败,那么整个事务都将回滚,在pl/sql中用户可以明确地使用commit、rollback、savepoint以及set transaction语句。
commit语句终止事务,永久保存数据库的变化,同时释放所有lock,rollback终止现行事务释放所有lock,但不保存数据库的任何变化,savepoint用于设置中间点,当事务调用过多的数据库操作时,中间点是非常有用的,set transaction用于设置事务属性,比如read-write和隔离级等。
显式游标
当查询返回结果超过一行时,就需要一个显式游标,此时用户不能使用select into语句。pl/sql管理隐式游标,当查询开始时隐式游标打开,查询结束时隐式游标自动关闭。显式游标在pl/sql块的声明部分声明,在执行部分或异常处理部分打开,取数据,关闭。下表显示了显式游标和隐式游标的差别:
表1 隐式游标和显式游标
隐式游标 显式游标
pl/sql维护,当执行查询时自动打开和关闭 在程序中显式定义、打开、关闭,游标有一个名字。
游标属性前缀是sql 游标属性的前缀是游标名
属性%isopen总是为false %isopen根据游标的状态确定值
select语句带有into子串,只有一行数据被处理 可以处理多行数据,在程序中设置循环,取出每一行数据。
使用游标
这里要做一个声明,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情况,我们所说的游标都是指显式游标。要在程序中使用游标,必须首先声明游标。
声明游标
语法:
cursor cursor_name is select_statement;
在pl/sql中游标名是一个未声明变量,不能给游标名赋值或用于表达式中。
例:
delcare
cursor c_emp is select empno,ename,salary
from emp
where salary>2000
order by ename;
........
begin
在游标定义中select语句中不一定非要表可以是视图,也可以从多个表或视图中选择的列,甚至可以使用*来选择所有的列 。
打开游标
使用游标中的值之前应该首先打开游标,打开游标初始化查询处理。打开游标的语法是:
open cursor_name
cursor_name是在声明部分定义的游标名。
例:
open c_emp;
关闭游标
语法:
close cursor_name
例:
close c_emp;
从游标提取数据
从游标得到一行数据使用fetch命令。每一次提取数据后,游标都指向结果集的下一行。语法如下:
fetch cursor_name into variable[,variable,...]
对于select定义的游标的每一列,fetch变量列表都应该有一个变量与之相对应,变量的类型也要相同。
例:
set serveriutput on
declare
v_ename emp.ename%type;
v_salary emp.salary%type;
cursor c_emp is select ename,salary from emp;
begin
open c_emp;
fetch c_emp into v_ename,v_salary;
dbms_output.put_line(''salary of employee''|| v_ename
||''is''|| v_salary);
fetch c_emp into v_ename,v_salary;
dbms_output.put_line(''salary of employee''|| v_ename
||''is''|| v_salary);
fetch c_emp into v_ename,v_salary;
dbms_output.put_line(''salary of employee''|| v_ename
||''is''|| v_salary);
close c_emp;
end
这段代码无疑是非常麻烦的,如果有多行返回结果,可以使用循环并用游标属性为结束循环的条件,以这种方式提取数据,程序的可读性和简洁性都大为提高,下面我们使用循环重新写上面的程序:
set serveriutput on
declare
v_ename emp.ename%type;
v_salary emp.salary%type;
cursor c_emp is select ename,salary from emp;
begin
open c_emp;
loop
fetch c_emp into v_ename,v_salary;
exit when c_emp%notfound;
dbms_output.put_line(''salary of employee''|| v_ename
||''is''|| v_salary);
end
记录变量
定义一个记录变量使用type命令和%rowtype,关于%rowstype的更多信息请参阅相关资料。
记录变量用于从游标中提取数据行,当游标选择很多列的时候,那么使用记录比为每列声明一个变量要方便得多。
当在表上使用%rowtype并将从游标中取出的值放入记录中时,如果要选择表中所有列,那么在select子句中使用*比将所有列名列出来要安全得多。
例:
set serveriutput on
declare
r_emp emp%rowtype;
cursor c_emp is select * from emp;
begin
open c_emp;
loop
fetch c_emp into r_emp;
exit when c_emp%notfound;
dbms_out.put.put_line(''salary of employee''||r_emp.ename||''is''|| r_emp.salary);
end loop;
close c_emp;
end;
%rowtype也可以用游标名来定义,这样的话就必须要首先声明游标:
set serveriutput on
declare
cursor c_emp is select ename,salary from emp;
r_emp c_emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into r_emp;
exit when c_emp%notfound;
dbms_out.put.put_line(''salary of employee''||r_emp.ename||''is''|| r_emp.salary);
end loop;
close c_emp;
end;
带参数的游标
与存储过程和函数相似,可以将参数传递给游标并在查询中使用。这对于处理在某种条件下打开游标的情况非常有用。它的语法如下:
cursor cursor_name[(parameter[,parameter],...)] is select_statement;
定义参数的语法如下:
parameter_name [in] data_type[{:=|default} value]
与存储过程不同的是,游标只能接受传递的值,而不能返回值。参数只定义数据类型,没有大小。
另外可以给参数设定一个缺省值,当没有参数值传递给游标时,就使用缺省值。游标中定义的参数只是一个占位符,在别处引用该参数不一定可靠。
在打开游标时给参数赋值,语法如下:
open cursor_name[value[,value]....];
参数值可以是文字或变量。
例:
decalre
cursor c_dept is select * from dept order by deptno;
cursor c_emp (p_dept varachar2) is
select ename,salary
from emp
where deptno=p_dept
order by ename
r_dept dept%rowtype;
v_ename emp.ename%type;
v_salary emp.salary%type;
v_tot_salary emp.salary%type;
begin
open c_dept;
loop
fetch c_dept into r_dept;
exit when c_dept%notfound;
dbms_output.put_line(''department:''|| r_dept.deptno||''-''||r_dept.dname);
v_tot_salary:=0;
open c_emp(r_dept.deptno);
loop
fetch c_emp into v_ename,v_salary;
exit when c_emp%notfound;
dbms_output.put_line(''name:''|| v_ename||'' salary:''||v_salary);
v_tot_salary:=v_tot_salary+v_salary;
end loop;
close c_emp;
dbms_output.put_line(''toltal salary for dept:''|| v_tot_salary);
end loop;
close c_dept;
end;
游标for循环
在大多数时候我们在设计程序的时候都遵循下面的步骤:
1、打开游标
2、开始循环
3、从游标中取值
4、检查那一行被返回
5、处理
6、关闭循环
7、关闭游标
可以简单的把这一类代码称为游标用于循环。但还有一种循环与这种类型不相同,这就是for循环,用于for循环的游标按照正常的声明方式声明,它的优点在于不需要显式的打开、关闭、取数据,测试数据的存在、定义存放数据的变量等等。游标for 循环的语法如下:
for record_name in
(corsor_name[(parameter[,parameter]...)]
| (query_difinition)
loop
statements
end loop;
下面我们用for循环重写上面的例子:
decalre
cursor c_dept is select deptno,dname from dept order by deptno;
cursor c_emp (p_dept varachar2) is
select ename,salary
from emp
where deptno=p_dept
order by ename
v_tot_salary emp.salary%type;
begin
for r_dept in c_dept loop
dbms_output.put_line(''department:''|| r_dept.deptno||''-''||r_dept.dname);
v_tot_salary:=0;
for r_emp in c_emp(r_dept.deptno) loop
dbms_output.put_line(''name:''|| v_ename||'' salary:''||v_salary);
v_tot_salary:=v_tot_salary+v_salary;
end loop;
dbms_output.put_line(''toltal salary for dept:''|| v_tot_salary);
end loop;
end;
在游标for循环中使用查询
在游标for循环中可以定义查询,由于没有显式声明所以游标没有名字,记录名通过游标查询来定义。
decalre
v_tot_salary emp.salary%type;
begin
for r_dept in (select deptno,dname from dept order by deptno) loop
dbms_output.put_line(''department:''|| r_dept.deptno||''-''||r_dept.dname);
v_tot_salary:=0;
for r_emp in (select ename,salary
from emp
where deptno=p_dept
order by ename) loop
dbms_output.put_line(''name:''|| v_ename||'' salary:''||v_salary);
v_tot_salary:=v_tot_salary+v_salary;
end loop;
dbms_output.put_line(''toltal salary for dept:''|| v_tot_salary);
end loop;
end;
游标中的子查询
语法如下:
cursor c1 is select * from emp
where deptno not in (select deptno
from dept
where dname!=''accounting'');
可以看出与sql中的子查询没有什么区别。
游标中的更新和删除
在pl/sql中依然可以使用update和delete语句更新或删除数据行。显式游标只有在需要获得多行数据的情况下使用。pl/sql提供了仅仅使用游标就可以执行删除或更新记录的方法。
update或delete语句中的where current of子串专门处理要执行update或delete操作的表中取出的最近的数据。要使用这个方法,在声明游标时必须使用for update子串,当对话使用for update子串打开一个游标时,所有返回集中的数据行都将处于行级(row-level)独占式锁定,其他对象只能查询这些数据行,不能进行update、delete或select...for update操作。
语法:
for update [of [schema.]table.column[,[schema.]table.column]..
[nowait]
在多表查询中,使用of子句来锁定特定的表,如果忽略了of子句,那么所有表中选择的数据行都将被锁定。如果这些数据行已经被其他会话锁定,那么正常情况下oracle将等待,直到数据行解锁。
在update和delete中使用where current of子串的语法如下:
where{current of cursor_name|search_condition}
例:
delcare
cursor c1 is select empno,salary
from emp
where comm is null
for update of comm;
v_comm number(10,2);
begin
for r1 in c1 loop
if r1.salary<500 then
v_comm:=r1.salary*0.25;
elseif r1.salary<1000 then
v_comm:=r1.salary*0.20;
elseif r1.salary<3000 then
v_comm:=r1.salary*0.15;
else
v_comm:=r1.salary*0.12;
end if;
update emp;
set comm=v_comm
where current of c1l;
end loop;
end
6. Oracle中的游标可以带参数么
可以啊,参数在游标定义时使用,打开时传入参数,例如:
create or replace procere a
as
cursor b(c_id int)is select * from d where id=c_id;
begin
open b(111);
end;
貌似就这样了。呵呵
7. 游标是什么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;
8. Oracle中调用输出参数为游标存储过程的SQL语句怎么写
create or replace procere test1(cursor1 out SYS_REFCURSOR)is
begin
open cursor1 for select * from 表名;
end;
然后在你调用的程序里去这个游标就可以了
9. Oracle中怎么用游标作为输出参数,写点代码举例吧!
您好,要自己定义一个type [cur_name] is ref cursor游标,返回的时候就直接 procere AAA(变量名 out [cur_name])如此申明OUT变量
存储过程 用系统默认的 sys_refcursor 游标类型 定义变量就OK了
1、Sql代码
--PL/SQL Code (包中带过程) 过程带游标的OUT参数,返回游标(ref cursor)
create or replace package my_pack as
type my_ref_cursor is ref cursor;
procere getMyCursor(val out my_ref_cursor);
end my_pack;
create or replace package body my_pack as
procere getMyCursor(val out my_ref_cursor)
is
begin
open val for select * from student;
end;
end my_pack;
--PL/SQL Code (包中带过程) 过程带游标的OUT参数,返回游标(ref cursor)
create or replace package my_pack as
type my_ref_cursor is ref cursor;
procere getMyCursor(val out my_ref_cursor);
end my_pack;
create or replace package body my_pack as
procere getMyCursor(val out my_ref_cursor)
is
begin
open val for select * from student;
end;
end my_pack;
2、Java代码
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call my_pack.getMyCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call my_pack.getMyCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}
3、Sql代码
--PL/SQL Code(存储过程) 带游标的OUT参数,返回游标(ref cursor)
create or replace procere retCursor(ret_cursor out sys_refcursor)is
ret_cursor_value sys_refcursor;
begin
open ret_cursor_value for select * from student;
ret_cursor:=ret_cursor_value;
end retCursor;
--PL/SQL Code(存储过程) 带游标的OUT参数,返回游标(ref cursor)
create or replace procere retCursor(ret_cursor out sys_refcursor)is
ret_cursor_value sys_refcursor;
begin
open ret_cursor_value for select * from student;
ret_cursor:=ret_cursor_value;
end retCursor;
4、Java代码
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call retCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@XX.XX.XX.XXX:XXXX:oracle9i","XXX_temp","XXX_temp");
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
CallableStatement cs = conn.prepareCall("{ call retCursor(?) }");
cs.registerOutParameter(1,OracleTypes.CURSOR);
cs.execute();
ResultSet rs = ((OracleCallableStatement)cs).getCursor(1);
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+ rs.getDate(4)+" "+rs.getString(5));
}
5、下面是个每个学生求平均值的存储过程。遇到的问题是带参数游标中的变量名字不要和表中的一样,否则会出问题
Sql代码
create or replace procere AAA
as
--查询学生表的ID
cursor s_sno is select s.sno from student s;
--通过学生ID查询平均成绩
cursor sc_avg(s_no varchar2) is select avg(sc.degree) from score sc where sc.sno=s_no;
s_sno_j student.sno%type; --变量ID
sc_avg_i score.degree%type; --变量平局成绩
begin
open s_sno;--打开查询ID的游标
loop
fetch s_sno into s_sno_j;
exit when s_sno%notfound;
open sc_avg(s_sno_j); --打开查询平均成绩的游标,参数为学生ID
loop
fetch sc_avg into sc_avg_i;
exit when sc_avg%notfound;
dbms_output.put_line(sc_avg_i);
end loop;
close sc_avg;
end loop;
close s_sno;
end AAA;