--普通类型的PL/SQL程序块
select *from emp
declare --定义部分(变量,常量(类型前加constant),游标)
v_ename varchar2(20);
v_date date;
v_sal number(20);
begin --执行部分(主要的sql语句和输出的信息)
--into(把查询到的值赋给已声明的量) &+任意字符(用来取用户输入的值)
select ename,hiredate,sal into v_ename,v_date,v_sal from emp where empno=&renyi;
dbms_output.put_line('员工姓名:'||v_ename);--Oracle中的输出语句
dbms_output.put_line('员工入职信息:'||to_char(v_date,'yyyy-mm-dd'));
dbms_output.put_line('员工的工资:'||v_sal);
exception --异常处理部分(没有查询到执行部分的信息时,执行异常处理中的话)
when no_data_found then dbms_output.put_line('没有该员工');
end;--结束部分
--处理基本的几个普通类型的变量还有四种特殊的变量声明(%type,%rowtype,record,table)
--%type变量类型(一个表的列名不知道是什么类型时用)
declare
v_ename emp.ename%type;
v_date emp.hiredate%type;
begin
select ename,hiredate into v_ename,v_date from emp where empno=7499;
dbms_output.put_line('员工姓名'||v_ename);
dbms_output.put_line('员工姓名'||to_char(v_date,'yyyy-mm-dd'));
exception
when no_data_found then dbms_output.put_line('没有改员工');
end;
--%rowtype变量类型(一张表有很多列rowtype可以表示表中的一行记录)
select *from dept
declare
v_dept_row dept%rowtype;
begin
select * into v_dept_row from dept where deptno=&ff;
dbms_output.put_line('员工编号'||v_dept_row.deptno);
dbms_output.put_line('员工姓名'||v_dept_row.dname);
dbms_output.put_line('员工上司'||v_dept_row.loc);
exception
when no_data_found then dbms_output.put_line('没有该员工');
end;
--record变量类型
select *from emp
declare
type emp_record_type is record --定义一个记录类型,包含一个员工的信息
(
ename emp.ename%type,
sal number(20)
);
v_emp_type emp_record_type; --定义记录类型变量
begin
select ename,sal into v_emp_type from emp where empno=ⅆ
dbms_output.put_line('员工姓名'||v_emp_type.ename);
dbms_output.put_line('员工工资'||v_emp_type.sal);
exception
when no_data_found then dbms_output.put_line('没有该员工信息');
end;
--table变量类型
select * from dept
declare
--定义一个table数据类型 值得类型为dept%rowtype 键为有符号的整数
type dept_table_type is table of dept%rowtype index by binary_integer;
v_dept_table dept_table_type; --定义table类型变量
begin
select * into v_dept_table(0) from dept where deptno=≪
dbms_output.put_line('编号'||v_dept_table(0).deptno||'姓名'||v_dept_table(0).dname||'所在地'||v_dept_table(0).loc);
exception
when no_data_found then dbms_output.put_line('没有该条信息');
end;
----------------------------------------流程控制---------------------------------------------------
--if语句(if 条件 then 输出语句;end if;)
declare
shu number(10):=&e;
begin
if shu>0 then dbms_output.put_line('它是大于0的');
end if;
end;
--if else语句(if 条件 then 输出语句1; else 输入语句2; end if;)
declare
shu number(10):=&e;
begin
if shu=1 then dbms_output.put_line('1');
else
dbms_output.put_line('不等于1');
end if;
end;
--if selif语句(if 条件 then 输出语句1;elsif 条件 then 输出语句2;else 输出语句3;end if)
declare
shu number(10):=&e;
begin
if shu=0 then dbms_output.put_line('0');
elsif shu=1 then dbms_output.put_line('1');
elsif shu=2 then dbms_output.put_line('2');
else dbms_output.put_line('不再这些范围');
end if;
end;
select * from tab_aaa
--if语句操作数据库
declare
v_aaa tab_aaa%rowtype;
begin
v_aaa.tid:=&e;
select * into v_aaa from tab_aaa where tid=v_aaa.tid;
dbms_output.put_line('更新前成绩'||v_aaa.tcj);
if v_aaa.tcj =70 then
update tab_aaa set tcj=tcj+20 where tid=v_aaa.tid;
else
if v_aaa.tcj =50 then
update tab_aaa set tcj=tcj+20 where tid=v_aaa.tid;
else
update tab_aaa set tcj=tcj+30 where tid=v_aaa.tid;
end if;
end if;
end;
--case语句(和java,switch语句很像)
declare
shu varchar2(10):=ⅆ
begin
case shu
when 0 then dbms_output.put_line('0');
when 1 then dbms_output.put_line('1');
when 2 then dbms_output.put_line('2');
end case;
exception
--case语句异常case_not_fount(语句中的选项与用户输入的数据不匹配)
when case_not_found then dbms_output.put_line('没有改信息');
end;
--循环控制
--loop循环(loop 条件 exit )
declare
shu number(10):=&e;
begin
loop
if shu>2 then exit; --当shu>2的时候跳出循环
end if;
dbms_output.put_line('niaho1'||shu);
shu:=shu+1;
end loop;
end;
select * from tab_aaa
--loop循环添加数据库中表的数据
declare
type v_aaa_type is table
of tab_aaa%rowtype
index by binary_integer;
shu number(5):=0;
v_aaa_table v_aaa_type;
begin
v_aaa_table(0).tid:=4;
v_aaa_table(0).tname:='a';
v_aaa_table(0).tcj:=88;
v_aaa_table(1).tid:=5;
v_aaa_table(1).tname:='a';
v_aaa_table(1).tcj:=78;
v_aaa_table(2).tid:=5;
v_aaa_table(2).tname:='a';
v_aaa_table(2).tcj:=78;
loop
if shu>2 then exit; --当shu>2的时候跳出循环
end if;
insert into tab_aaa values(v_aaa_table(shu).tid,v_aaa_table(shu).tname,v_aaa_table(shu).tcj);
shu:=shu+1;
end loop;
end;
--while循环
declare
v_shu number(10):=&e;
begin
while v_shu<=2 loop
dbms_output.put_line('你好'||v_shu);
v_shu:=v_shu+1;
end loop;
end;
select *from tab_aaa
--while循环
declare
type v_aaa_type is table
of tab_aaa%rowtype
index by binary_integer;
shu number(5):=0;
v_aaa_table v_aaa_type;
begin
v_aaa_table(0).tid:=4;
v_aaa_table(0).tname:='a';
v_aaa_table(0).tcj:=88;
v_aaa_table(1).tid:=5;
v_aaa_table(1).tname:='a';
v_aaa_table(1).tcj:=78;
v_aaa_table(2).tid:=5;
v_aaa_table(2).tname:='a';
v_aaa_table(2).tcj:=78;
while
shu<=2 loop
insert into tab_aaa values(v_aaa_table(shu).tid,v_aaa_table(shu).tname,v_aaa_table(shu).tcj);
shu:=shu+1;
end loop;
end;
--for循环
declare
a number(30):=&ea;
begin
for i in 1 .. a loop
sys.dbms_output.put_line('bbb' || i);
end loop;
commit;
end;
select *from tab_aaa
--for循环操作数据库(reverse)
declare
type v_aaa_type is table
of tab_aaa%rowtype
index by binary_integer;
shu number(5):=0;
v_aaa_table v_aaa_type;
begin
v_aaa_table(0).tid:=4;
v_aaa_table(0).tname:='a';
v_aaa_table(0).tcj:=88;
v_aaa_table(1).tid:=5;
v_aaa_table(1).tname:='a';
v_aaa_table(1).tcj:=78;
v_aaa_table(2).tid:=5;
v_aaa_table(2).tname:='a';
v_aaa_table(2).tcj:=78;
for shu in 0..v_aaa_table.count-1 loop
insert into tab_aaa values(v_aaa_table(shu).tid,v_aaa_table(shu).tname,v_aaa_table(shu).tcj);
end loop;
end;
--事务(commit:提交,savepoint:保存点,rollback:回滚)
begin
inset into emp(emp,ename) values(11,'asd');
inset into emp(emp,ename) values(12,'asd');
savepoint a;
inset into emp(emp,ename) values(13,'asd');
rollback to a;
inset into emp(emp,ename) values(14,'asd');
commit;
end;
----------------------------------------PL/SQL程序异常-------------------------------------------
/*
access_into_null: 试图访问一个初始化的对象
case_not_found: case语句中的选项与用户输入的数据不匹配
cursor_already_open: 试图打开一个已打开的游标
dup_val_on_index: 试图打开一个唯一性的约束
invalid_cursor 试图打开一个无效的图标
invalid_number: 试图对非数字的值进行数据操作
login_denied: 无效的用户名或口令(密码)
no_data_fount: 查询未找到数据
not_logged_on: 还未链接就试图数据库操作
program_erroe: 内部错误
*/
declare
v_emp emp.ename%type;
begin
for i in 1..5 loop
select ename into v_emp from (select ename,rownum r from emp order by empno) where r=i;
dbms_output.put_line(v_emp);
end loop;
end;
PL/SQL语言
最新推荐文章于 2024-04-26 15:51:14 发布