设为首页 收藏本站
查看: 542|回复: 0

[经验分享] Oracle_PL/SQL的基本语法操作

[复制链接]

尚未签到

发表于 2016-8-10 07:15:40 | 显示全部楼层 |阅读模式
declare
temp_name varchar2(20):='hello world';
temp_num int :=1;
begin
--set serveroutput on
dbms_output.put_line(temp_num||'==='||temp_name);
end;
---if
declare
temp_num int :=&请输入num的值;
begin
if temp_num>5 then
dbms_output.put_line('temp_num>5');
elsif temp_num=5 then
dbms_output.put_line('temp_num==5');
else
dbms_output.put_line('temp_num<5');
end if;
end;
---case
declare
score char:='&num';
begin
case score
when 'A' then
dbms_output.put_line('very good');
when 'B' then
dbms_output.put_line('is good');
when 'C' then
dbms_output.put_line('is normal');
when 'D' then
dbms_output.put_line('is bad');
else
dbms_output.put_line('input a value');
end case;
end;
--loop
declare
temp_num int :=1;
begin
loop
if(temp_num>=10) then--出循环条件
exit;
end if;
dbms_output.put_line(temp_num);
temp_num:=temp_num+1;
end loop;
end;
--while
declare
temp_num int :=1;
begin
while temp_num<10 loop--进入循环条件
dbms_output.put_line(temp_num);
temp_num:=temp_num+1;
end loop;
end;
--for
declare
temp_num int :=1;
begin
for temp_num in  reverse 1..10 loop
dbms_output.put_line(temp_num);
end loop;
end;
---动态sql--ddl
declare
tbName varchar2(10):='my_tab';
begin
execute immediate '
create table '||tbName||' (
id int,
name varchar2(10),
age int
)';
end;

---绑定参数
declare
id int:=1;
tbName varchar2(10):='my_tab';
age int:=11;
uname varchar2(10):='my_tab';
begin
execute immediate
'insert into my_tab values(:1,:abc,:2) returning name into :uname'
using id,tbName,age returning into uname
;
end;
--execute immediate 字符串 using 输入参数 returning into 返回值参数
/*
Excute immediate 动态SQL语句 using 绑定参数列表 returning into 输出参数列表;
DML = Data Manipulation Language 数据操作语句[m'nipjulbl]
DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言
DCL(Data Control Language):.?是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL
TCL(Transaction Control Language)事务控制语言 .SAVEPOINT 设置保存点 .ROLLBACK? 回滚 .SET TRANSACTION .
使用动态SQL原因
1.业务:只能在运行时才能确定sql。
2.在开发动态拼装SQL,更灵活,更强大。
3.更容易维护。
*/
----系统exception
declare
uname varchar2(10);
begin
select name into uname from users where id=2;
case '22'
when '1' then uname:='1';
when '2' then uname:='2';
end case;
dbms_output.put_line(uname);
exception
when too_many_rows then
dbms_output.put_line('查询到多行数据');
when others then
dbms_output.put_line('异常');
end;
---自定义异常
declare
n int:=&5;
e exception;--定义
begin
if(n=5) then
raise e;--手动抛出
end if;
exception--处理
when e then
dbms_output.put_line('自定义异常');
end;
---隐式游标
declare
uname varchar2(10);
begin
select name into uname from users where id=2;
dbms_output.put_line('查询到'||sql%rowcount);
if(sql%found)then
dbms_output.put_line('found is true');
else
dbms_output.put_line('found is false');
end if;
if(sql%notfound)then
dbms_output.put_line('notfound is true');
else
dbms_output.put_line('notfound is false');
end if;
if(sql%isopen)then
dbms_output.put_line('isopen is true');
else
dbms_output.put_line('isopen is false');
end if;
end;
---%type(字段类型)
---%rowtype
declare
uname users.name%type;
myrow users%rowtype;--行类型
begin
select name into uname from users where id=2;
select * into myrow from users where id=2;
dbms_output.put_line(uname);
dbms_output.put_line(myrow.name||'==='||myrow.password);
end;
---显示游标
declare
cursor my_cursor  is
select * from users;--声明
myrow users%rowtype;
begin
open my_cursor;--打开
if(my_cursor%notfound) then
dbms_output.put_line('无数据');
else
fetch my_cursor into myrow;--取数据
dbms_output.put_line(myrow.id||'==='||myrow.name||'==='||myrow.password);
end if;
close my_cursor;--关闭
end;
--while循环
declare
cursor my_cursor  is
select * from users;--声明
myrow users%rowtype;
begin
open my_cursor;--打开
fetch my_cursor into myrow;--取数据
while (my_cursor%found) loop
dbms_output.put_line(myrow.id||'==='||myrow.name||'==='||myrow.password);
fetch my_cursor into myrow;
end loop;
close my_cursor;--关闭
end;
--for循环
declare
cursor my_cursor  is
select * from users;--声明
myrow users%rowtype;
begin
for myrow in my_cursor loop
dbms_output.put_line(myrow.id||'==='||myrow.name||'==='||myrow.password);
end loop;
end;
--loop循环
declare
cursor my_cursor is select * from info;
myrow info%rowtype;
begin
open my_cursor;
fetch my_cursor into myrow;
loop
if my_cursor%found then
dbms_output.put_line(myrow.id);
fetch my_cursor into myrow;
else
exit;
end if;
end loop;
close my_cursor;
end;

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-255585-1-1.html 上篇帖子: 安装oracle数据库时用 下篇帖子: oracle服务启动、关闭脚本(windows下)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表