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;
第七章 存储过程(Procedure)和包(Package)
SQLServer - 存储过程 (output参数 return)
Oracle -存储过程(out参数) 函数(return)
--1.存储过程
create or replace procedure print(msg varchar2)
is
begin
dbms_output.put_line(msg);
end;
--1.1 调用存储过程
execute print('helloworld');
--1.2
create or replace procedure 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 procedure 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 procedure 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
procedure print(msg varchar2);
function findEmp(p_empno number)
return emp.ename%type;
end;
create or replace package body mypkg
is
procedure 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;
我上学时候练习用的
分享到:
相关推荐
voracle PL/SQL
pl/sql存储过程,函数,游标,以及存储过程中的基础知识,绝对值得你收藏的经典题目,让你的pl/sql得到最大的锻炼。让你的数据库逻辑更加灵活。
除了编程之外,本书还提供了有关开发工具SQL Developer的使用的工具建议,采用数据库环境中的最佳实践并保护PL / SQL代码中的易受攻击区域以避免代码注入。 关于作者 Saurabh K. Gupta是一位经验丰富的数据库技术...
本书是一本逐步分解的,详尽的pl/sql编程教程,使用真实场景的试验、范例和练习来介绍读者所需的pl/sql编程技能,涵盖oracle 11g的最新特性。作者的写作手法源自于在哥伦比亚大学教授pl/sql编程技术的经验,深度...
Oracle SQL PL/SQL 练习资料,非常实用的练习资源。入门必备
Oracle存储过程LP/SQL练习题(含答案) 几个练习题
《oracle database 11gpl/sql编程实战》通过动手练习、大量的示例以及实际的项目帮助读者掌握pl/sql。书中包含大量最佳实践,涵盖了pl/sql语言所有的最新功能和改进之处。每章末尾配有测验题,可以帮助读者进一步...
1. 1Z0-001 PL/SQL 4本(这里是分章放在PLSQL目录中的) 2. 1Z0-023 DBA 2本 Architecture And Administration V1 Architecture And Administration V2 3. 1Z0-025 Backup & Recover 2本 Backup And Recovery Workshop...
NULL 博文链接:https://chaoyi.iteye.com/blog/2147401
1、简单程序 2、限定查询 3、多表查询 4、子查询 5、分页查询 6、单行函数 7、oracle对象操作 8、oracle类型练习 9、java中调用oracle 10、procedure经典练习
这是一些oracle数据库的常见问题的解答范例,对初学者很有用的
oracle pl/sql 存储过程 练习程序 oracle pl/sql 存储过程 练习程序 oracle pl/sql 存储过程 练习程序 oracle pl/sql 存储过程 练习程序
oracle pl/sql 存储过程 oracle pl/sql 存储过程 oracle pl/sql 存储过程 oracle pl/sql 存储过程 oracle pl/sql 存储过程 单元开发习题
上个礼拜做个部门sql练习题,本来写好了调试通了,第二天来上班代码没了,新员工不知道误删了。 以上上令人郁闷的事情是我亲身经历啊。所以想找个解决方法,eclipse大家都用管了,可以连接cvs进行代码同步,check in...
1.练习利用SQL*Plus编写、执行PL/SQL程序的命令。 2.记录执行命令和操作过程中遇到的问题及解决方法,注意从原理上解释原因。 3.练习利用PLSQL Developer编写和管理存储过程、存储函数和触发器等。
oracle练习题,Oracle基础知识。oracle练习题,Oraoracle练习题,Oracle基础知识。cle基础知识。
主要是为了介绍oracle数据库系统下强大的PL/SQL编程语言,主要结合实例讲解,学完之后能对使用PL/SQL语言在企业开发中进行编程有了初步认识
详细讲解了PLSQL的基本语法,以及储存过程、函数、触发器、游标和包。文档中还包括例子和练习。
OraclePL/SQL练习,基本的OraclePL/SQL的语法,游标的在PL/SQL中的使用,如何声明游标,打开游标,提取记录,关闭游标。