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 ...
随机推荐
- python的enumerate函数
python的enumerate函数用于循环索引和元素 例如 foo = 'abc' for i , ch in enumerate(foo): print ch, '(%d)' % i 输出结果: ...
- Centos 7配置docker-阿里云镜像加速
阿里云加速网址:https://cr.console.aliyun.com/cn-hangzhou/mirrors(自行注册账密码) sudo mkdir -p /etc/docker sudo vi ...
- JDBC连接数据库反射实现O/R映射
测试preparedStatement public void testPreparedStatement(){ Connection connection=null; PreparedStateme ...
- Python 爬虫 ajax爬取马云爸爸微博内容
ajax爬取情况 有时候我们在用 Requests 抓取页面的时候,得到的结果可能和在浏览器中看到的是不一样的,在浏览器中可以看到正常显示的页面数据,但是使用 Requests 得到的结果并没有,这其 ...
- 沉淀再出发:OpenStack初探
沉淀再出发:OpenStack初探 一.前言 OpenStack是IaaS的一种平台,通过各种虚拟化来提供服务.我们主要看一下OpenStack的基本概念和相应的使用方式. 二.OpenStack的框 ...
- [原]Ubuntu 下安装apache+PHP
1.安装apache2 sudo apt-get install apache2 运行如下命令重启:sudo /etc/init.d/apache2 restart 在浏览器里输入http://loc ...
- video设置视频的播放位置(本例中实现效果是视频第一次播放完成后,接下来中从视频的中间部位开始循环播放)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Guava包学习---Bimap
Bimap也是Guava中提供的新集合类,别名叫做双向map,就是key->value,value->key,也就是你可以通过key定位value,也可以用value定位key. 这个场景 ...
- 6、Android---运用手机多媒体(待完成)
6.1.程序运行在手机上 6.2.使用通知 通知是Android中比较由特色的一个功能 当某个应用程序需要向用户发出一些提示信息时 而该程序由不在前台的显示 就可以借助通知来实现 6.2.1.通知的基 ...
- 该网页已屏蔽以下插件Adobe Flash Player
2017.6.9更新:多谢网友留言,该网页已屏蔽以下插件Adobe Flash Player解决方法:chrome://flags/#run-all-flash-in-allow-mode选择启用就O ...