1. oracle存储过程中循环for in是如何使用的
1、首先编写存储过程的整体结构,如下图所示定义变量。
2. 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;
我上学时候练习用的
3. oracle数据库中循环同义词,怎么处理
1、 当用程序连接或者用plsql查询同义词时,如果出现ora-01775:同义词的循环链这样的问题。
一般是因为存在同义词,但同义词没有相应的对象。
2、 先查有没有循环的同义词。
select * from dba_synonyms
where table_owner='TEST'
and synonym_name<>table_name;
没有记录。
3、 再查同义词没有对象的数据库对象
select * from dba_synonyms
where table_owner='TEST'
and
synonym_name in
(select a.synonym_name from dba_synonyms a where a.table_owner='TEST'
minus
select object_name from user_objects)
4、 把查询出来的结果进行查询表
select * from DRILL_PRESON
如果该同义词没有相应的对象,则会包ora-01775的错误
5、 把这个同义词删除
drop public synonym DRILL_PRESON
4. 写PLSQL语句(或存储过程)循环insert实现数据的复制
先试试这个,好久不写了,一会儿找个环境帮你试试,补0的问题一会儿再解决
create table B as select * from A where 1=0;
declare
cursor ind is select * from A ;
begin
for cur in ind
loop
for num 1..2
loop
insert into B values ( cur.id||num, cur.name||num, note );
end loop;
end loop;
end ;
5. Oracle中使用PL/SQL怎样用循环插入多条数据
使用loop循环,比如:
for item in (select a,b,c from table_a where 条件) loop
insert into table_b(a,b,c) values (item.a,item.b,item.c);
end loop;
也可以使用索引表循环,以上只是一个简单的例子,需要根据你的具体情况选择循环方式。
6. Oracle 11g的PLSQL部分
·结果集缓存(Result Set Caching)
这一特性能大大提高很多程序的性能。在一些MIS系统或者OLAP系统中,需要使用到很多select count(*)这样的查询。在之前,我们如果要提高这样的查询的性能,可能需要使用物化视图或者查询重写的技术。在11g,我们就只需要加一个 /*+result_cache*/的提示就可以将结果集缓存住,这样就能大大提高查询性能。当然,在这种情况下,我们可能还要关心另外一个问题:完整性。因为在oracle中是通过一致性读来保证数据的完整性的。而显然,在这种新特性下,为提高性能,是从缓存中的结果集中读取数据,而不会从回滚段中读取数据的。关于这个问题,答案是完全能保证完整性。因为结果集是被独立缓存的,在查询期间,任何其他DML语句都不会影响结果集中的内容,因而可以保证数据的完整性。
·对象依赖性改进
在11g之前,如果有函数或者视图依赖于某张表,一旦这张表发生结构变化,无论是否涉及到函数或视图所依赖的属性,都会使函数或视图变为invalid。在11g中,对这种情况进行了调整:如果表改变的属性与相关的函数或视图无关,则相关对象状态不会发生变化。
·正则表达式的改进
在10g中,引入了正则表达式。这一特性大大方便了开发人员。11g,oracle再次对这一特性进行了改进。其中,增加了一个名为regexp_count的函数。另外,其他的正则表达式函数也得到了改进。
·新SQL语法 =>
我们在调用某一函数时,可以通过=>来为特定的函数参数指定数据。而在11g中,这一语法也同样可以出现在sql语句中了。例如,你可以写这样的语句:
select f(x=>6) from al;
·对TCP包(utl_tcp、utl_smtp…)支持FGAC(Fine Grained Access Control)安全控制
·增加了只读表(read-only table)
在以前,我们是通过触发器或者约束来实现对表的只读控制。11g中不需要这么麻烦了,可以直接指定表为只读表。
·触发器执行效率提高了
·内部单元内联(Intra-Unit inlining)
在C语言中,你可以通过内联函数(inline)或者宏实现使某些小的、被频繁调用的函数内联,编译后,调用内联函数的部分会编译成内联函数的函数体,因而提高函数效率。在11g的plsql中,也同样可以实现这样的内联函数了。
·设置触发器顺序
可能在一张表上存在多个触发器。在11g中,你可以指定它们的触发顺序,而不必担心顺序混乱导致数据混乱。
·混合触发器(compound trigger)
这是11g中新出现的一种触发器。她可以让你在同一触发器中同时具有申明部分、before过程部分、after each row过程部分和after过程部分。
·创建无效触发器(Disabled Trigger)
11g中,开发人员可以可以闲创建一个invalid触发器,需要时再编译她。
·在非DML语句中使用序列(sequence)
在之前版本,如果要将sequence的值赋给变量,需要通过类似以下语句实现:
select seq_x.next_val into v_x from al;
在11g中,不需要这么麻烦了,下面语句就可以实现:
v_x := seq_x.next_val;
·PLSQL_Warning
11g中。可以通过设置PLSQL_Warning=enable all,如果在when others没有错误爆出就发警告信息。
·PLSQL的可继承性
可以在oracle对象类型中通过super(和java中类似)关键字来实现继承性。
·编译速度提高
因为不再使用外部C编译器了,因此编译速度提高了。
·改进了DBMS_SQL包
其中的改进之一就是DBMS_SQL可以接收大于32k的CLOB了。另外还能支持用户自定义类型和bulk操作。
·增加了continue关键字
在PLSQL的循环语句中可以使用continue关键字了(功能和其他高级语言中的continue关键字相同)。
·新的PLSQL数据类型——simple_integer
这是一个比pls_integer效率更高的整数数据类型。
7. 请教大神,oracle数据库循环语句怎么写
你想要的这几个结果,都可以直接使用SQL语句查出,无需循环。
Oracle循环实在PLSQL块中编写:关键字for XXX loop 循环体 end loop;
8. 如何向oracle 数据库循环录入数据
INSERT INTO table
SELECT 'aaa' || ROWNUM, 'aaa' FROM DUAL CONNECT BY ROWNUM <= 100
其中 100是你想插入的记录条数