Oracle之plsql及游标
--1、赋值
--:= 赋值
declare
var_name varchar2(10) :='&请输入名字';--&是一个提示输入的特殊符号,会打开一个输入框
var_age number(3) :='&请输入年龄';
begin
dbms_output.put_line(var_name||'---'||var_age);--输入 ||是连接符号和java中的+一样
end; --into 赋值
declare
var_name varchar2(10);
var_age number(3);
begin
select stuname,age into var_name,var_age from t_student where id = 2;
dbms_output.put_line(var_name||'---'||var_age);
end; --2、 特殊类型:
--%type 绑定某个表中的特定字段的类型
declare
v_name emp.ename%type;
v_job emp.job%type;
begin
select ename,job into v_name,v_job from emp where emp.empno=3;
dbms_output.put_line(v_name||'---'||v_job);
end; --%rowtype:行类型和表中的一行对应
declare
v_emp emp%rowtype; -- v_emp的类型是一个行类型 和emp的一条记录对应
begin
select * into v_emp from emp where empno=1;
dbms_output.put_line(v_emp.ename||' '||v_emp.sal||' '||v_emp.job);
end; --3、if条件判断
if语句
语法格式: if 条件 then
[ elsif 条件 then ]
[ elsif 条件 then]
[else ]
end if; --实例
declare
v_age number(3) :=&请输入年龄;
begin
if v_age = 18 then
dbms_output.put_line('');
elsif v_age > 18 then
dbms_output.put_line('大于18');
else dbms_output.put_line('小于18');
end if;
end; --4、case
case when 条件 then
when 条件 then
else
end case; --实例
declare
v_age number(3) :=&请输入年龄;
begin
case
when v_age = 18 then dbms_output.put_line('');--条件可以是一个定值也可以是>或者<
when v_age = 19 then dbms_output.put_line('');
when v_age = 20 then dbms_output.put_line('');
when v_age = 21 then dbms_output.put_line('');
when v_age = 22 then dbms_output.put_line('');
when v_age > 23 then dbms_output.put_line('大于23');
else dbms_output.put_line('不知道');
end case;
end; --5、循环
--5、1无限循环
loop
-- 循环体
exit when 退出条件;
end loop; --实例
declare
v_num number(3) := 1;
begin
loop
dbms_output.put_line(v_num);
v_num := v_num+1;
exit when v_num > 10;
end loop;
end; --5、2 while带条件的循环
while 循环条件 loop
--循环体
end loop; --实例
declare
v_i number(5) := 1;
begin
while v_i<10 loop
dbms_output.put_line(v_i);
v_i := v_i+1;
end loop;
end; --5/3 for循环:
--1.不用专门去声明循环变量
--2.每次只能自增一,
--3.要想实现循环降序需要在 in 后加 reverse --实例
declare
begin
for v_i in reverse 1..9 loop
dbms_output.put_line(v_i);
end loop;
end; --6、goto关键字:跳转到指定的位置 --实例:
declare
v_num number(5) := &请输入; begin
if v_num = 18 then
dbms_output.put_line(18);
goto a1;
elsif v_num = 20 then
dbms_output.put_line(20);
goto a2;
else dbms_output.put_line('----------');
goto a3;
end if; <<a1>>
dbms_output.put_line('a1======');
<<a2>>
dbms_output.put_line('a2======');
<<a3>>
dbms_output.put_line('a3======');
end; --7、动态SQL语句:解决的是字符串格式的sql语句执行的问题
EXECUTE IMMEDIATE dynamic_sql_string
[INTO define_variable_list]
[USING bind_argument_list]; declare
v_stuname t_student.stuname%type;
v_sql varchar2(100) := 'select stuname from t_student where id = :id';
v_id number(3) := &请输入查询的id;
begin
execute immediate v_sql into v_stuname using v_id;
dbms_output.put_line(v_stuname);
end; --8、异常处理
--8、1 系统异常
no_data_found;没有找到数据的异常
too_many_rows: 多行数据
others 其他异常 declare
v_stuname t_student.stuname%type;
v_id t_student.id%type;
begin
-- 业务逻辑代码
v_id:=100;
select stuname into v_stuname from t_student where id = v_id;
dbms_output.put_line(v_stuname);
-- 异常处理代码 exception
when no_data_found then
dbms_output.put_line('找不到数据');
when too_many_rows then
dbms_output.put_line('数据过多');
when others then
dbms_output.put_line('其他异常');
end; --8、2 自定义异常
declare
v_i number(3) :=&请输入;
myexception exception;
begin
if v_i = 2 then
raise myexception;
end if;
dbms_output.put_line('=================='); exception
when myexception then
dbms_output.put_line('自定义异常触发了');
when others then
dbms_output.put_line('不知道的异常');
end; 游标:
一.隐式游标:系统自动维护的,在我们做DML操作的时候系统会自动的维护这样一个游标
名称叫:sql
隐式游标提供的常用的属性:
sql%found: boolean 找到数据 true
sql%notfound: boolean 没有找到数据 true
sql%rowcount: 数值型 影响的行数
sql%isopen: 是否打开 dml中都是 false
插入:select * from student; begin
update t_student set stuname='xxxx' where id = 10; if sql%found then
dbms_output.put_line('影响了'|| sql%rowcount ||'行记录');
end if;
if sql%isopen then
dbms_output.put_line('--------1--------');
end if;
if sql%notfound then
dbms_output.put_line('-----------2-----------');
end if; end; 二.显示游标:处理多行数据,隐式游标配合显示游标使用
2.1无参游标
查询出学生表中的所有的记录:
使用的步骤:
1.声明游标
2.打开游标
3.循环提取数据
4.关闭游标 declare
v_student t_student%rowtype;
--1.声明游标
cursor mycursor is select * from t_student;
v_count number(3):=0;
begin
--2.打开游标
open mycursor;
--3.循环提取数据 loop
--提取数据
-- 每循环一次从游标中取一条记录保存到v_student变量中
fetch mycursor into v_student;
--指定退出条件
exit when mycursor%notfound;
v_count := v_count+1;
dbms_output.put_line(v_student.id||v_student.stuname||v_student.sex);
end loop;
dbms_output.put_line('有'||v_count||'记录');
--4.关闭游标
close mycursor;
end; declare
v_student t_student%rowtype;
cursor mycursor is select * from student for update; -- 1. for update
begin
open mycursor;
loop
fetch mycursor into v_student; exit when mycursor%notfound;
dbms_output.put_line(v_student.id||v_student.name||v_student.sex||v_student.birth);
if v_student.birth is null then
-- 2.在更新语句后加 current of 游标名称
update t_student set stuname='xxxx' where current of mycursor;
end if;
end loop;
commit;
close mycursor;
end; 2.2 有参游标
根据姓名查询学生表中的所有的学生信息
declare
v_student t_student%rowtype;
v_name t_student.stuname%type:='&请输入姓名';
cursor mycursor(c_name varchar2)
is select * from t_student where stuname like '%'||c_name||'%';
begin
open mycursor(v_name);
loop
fetch mycursor into v_student;
if mycursor%found then
dbms_output.put_line(v_student.id||'--'||v_student.stuname||'---'||v_student.age);
else
exit;
end if;
end loop;
close mycursor;
end; declare
v_student t_student%rowtype;
v_name t_student.stuname%type := '&请输入要查询的姓名';
cursor mycursor -- 带有参数
is select * from t_student where stuname like '%'||v_name||'%';
begin
open mycursor; --打开的时候需要指定参数
loop
fetch mycursor into v_student;
if mycursor%found then
-- 有数据
dbms_output.put_line(v_student.id||v_student.stuname||v_student.sex);
else
-- 退出
exit;
end if;
end loop;
close mycursor;
end; 2.3 游标循环时使用for循环提取 declare
v_name t_student.stuname%type := '&请输入';
cursor mycursor is select * from t_student where stuname like '%'||v_name||'%';
begin
for v_student in mycursor loop
update t_student set age=23;
dbms_output.put_line(v_student.age||v_student.stuname||v_student.sex);
end loop;
commit;
end; 3.REF游标【动态游标】:是解决游标动态执行sql
显示游标在声明的时候就必须制定sql语句
动态游标:在打开的时候确定要执行的sql语句比显示游标更加的灵活
缺点:不能使用for循环和更新行 3.1 自定义ref游标
通过REF游标查询出学生表中的所有的学生记录
declare
type myreftype is ref cursor;-- 1.定义一个ref 类型
myrefcursor myreftype;-- 2.声明一个myreftype类型的变量
v_student t_student%rowtype;
v_sql varchar2(100);
begin
v_sql:='select * from t_student'; -- for 后及可以带'' 也可以直接是sql语句
--open myrefcursor for select * from student; -- 打开游标的同时指定要执行的sql语句
open myrefcursor for v_sql;
loop
fetch myrefcursor into v_student;
exit when myrefcursor%notfound;
dbms_output.put_line(v_student.age||v_student.stuname||v_student.sex); end loop;
close myrefcursor;
end; 3.2 sys_refcursor:系统提供的一个 ref cursor 类型 declare
myrefcursor sys_refcursor; -- 声明一个变量类型是 refcursor 类型
v_student t_student%rowtype;
v_sql varchar2(100);
begin
v_sql :='select * from t_student';
open myrefcursor for v_sql;
loop
fetch myrefcursor into v_student;
exit when myrefcursor%notfound;
dbms_output.put_line(v_student.age||v_student.stuname||v_student.sex); end loop;
close myrefcursor;
end;
Oracle之plsql及游标的更多相关文章
- 2018.5.30 Oracle数据库PLSQL编程---游标的使用
显示游标的步骤 /* 显示游标处理步骤 1.声明游标 语法结构:cursor 游标名称 is SQL 语句; 2.打开游标 语法结构:open游标名称; 3.提取数据 语法结构:fetch 4.关闭游 ...
- oracle教程:PLSQL常用方法汇总
oracle教程:PLSQL常用方法汇总 在SQLPLUS下,实现中-英字符集转换alter session set nls_language='AMERICAN';alter session set ...
- 本地不安装oracle,PLsql远程连接
Oracle的Instant client工具包可以很好地解决本地不安装oracle,PLsql远程连接. 1.首先到Oracle网站下载Instant Client : http://www.ora ...
- oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 --------关闭防火墙试试
oracle用plsql登陆出错,提示ORA-12170:TNS:链接超时 但是使用sqlplus可以连接 ping 本机127.0.0.1 显示一般故障 后关闭防火墙,问题解决. ps:登录时使用@ ...
- Oracle在plsql中修改数据
Oracle在plsql中想要修改数据,有两种方式: a.使用rowid+点击锁图标,语句为: select t.*,rowid from T_BIC_PLY_MAIN t; b.使用for up ...
- oracle(sql)基础篇系列(五)——PLSQL、游标、存储过程、触发器
PL/SQL PL/SQL 简介 每一种数据库都有这样的一种语言,PL/SQL 是在Oracle里面的一种编程语言,在Oracle内部使用的编程语言.我们知道SQL语言是没有分支和循环的,而PL语 ...
- .Net程序员学用Oracle系列(27):PLSQL 之游标、异常和事务
1.游标 1.1.游标属性 1.2.隐式游标 1.3.游标处理及案例 2.异常 2.1.异常类别 2.2.异常函数 2.3.异常处理及案例 3.事务 3.1.开始事务.结束事务 3.2.自治事务 3. ...
- oracle(sql)基础篇系列(五)——PLSQL、游标、存储过程、触发器
PL/SQL PL/SQL 简介 每一种数据库都有这样的一种语言,PL/SQL 是在Oracle里面的一种编程语言,在Oracle内部使用的编程语言.我们知道SQL语言是没有分支和循环的,而PL语言是 ...
- 2018.6.5 Oracle plsql编程 游标的使用
--3.查询10部门所有员工的姓名.(ref游标实现) 动态游标 declare --创建一种游标类型 type type_cursor is ref cursor; --声明变量指定游标类型 v_c ...
随机推荐
- 京东原来你运用的这玩意,不错,我也要!! ContainerDNS
转自社区 ContainerDNS 本文介绍的 DNS 命名为 ContainerDNS,作为京东商城软件定义数据中心的关键基础服务之一,具有以下特点: 分布式,高可用 自动发现服务域名 后端探活 易 ...
- ProxySQL读写分离
我们首先看一下自己的环境: MHA已经搭建: master: slave: slave: MHA manager在172.16.16.34,配置文件如下: [root@localhost bin]# ...
- MySQL案例05:CPU负载优化
最近有套系统数据库周末总是告警,CPU使用率超过90%,开始由开发那边再跟进处理,我也就没参与,后来发现没进展就登录上去看了下,然后进行了部分优化,优化后效果还是比较明显的,具体优化过程本文会做详细的 ...
- Win10开启PIN码使用教程
很多电脑爱好者对于Win10内置的PIN码功能不太了解,很多朋友都还没有使用.其实,创建PIN码可以提到密码使用,当你登录到Windows和其它应用服务时,可以通过PIN码替代输入账户密码,提升安全性 ...
- December 30th 2016 Week 53rd Friday
Life without love is like a tree without blossoms or fruit. 缺少爱的生活就像从未开花结果的枯树. Love is not only the ...
- POJ 2528 Mayor's poster
主要是参考了这个博客 地址戳这儿 题目大意:n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) .求出最后还能看 ...
- [T-ARA][그녀를 보면][看着那个女人的话]
歌词来源:http://music.163.com/#/song?id=29343995 作曲 : 코난 [作曲 : Ko-nan] 作词 : 코난/로코 [作词 : Ko-nan-/lo-Ko] b ...
- MySQL提权之user.MYD中hash破解方法
经常在服务器提权的时候,尤其是windows环境下,我们发现权限不高,却可以读取mysql的datadir目录,并且能够成功下载user.MYD这个文件.但是在读取内容的时候,经常会遇到root密码h ...
- canvas图形库
总结了一些canvas绘制2d图形的方法,记录在博客中,以便需要的同学参考,也便于日后加深记忆. 1. 圆角矩形: 如上图:w表示矩形的宽,h表示矩形的高,r表示矩形圆角的半径.整个矩形在画布中,(0 ...
- Java并发案例04---Future和 FutureTask
4.Future和 FutureTask 4.1 Future是Callable的返回结果. 它有三个功能 1.判断任务是否完成 2.能够中断任务 3.能够获取任务返回结果 4.2 FutureTas ...