笔记plsql

来源:互联网 发布:java输入整数异常 编辑:程序博客网 时间:2024/06/04 18:11
declare 
  v_ename varchar2(50);
   begin 
    select ename into v_ename from emp where empno=&eno;
   dbms_output.put_line('员工名称:'||v_ename);
   exception 
   when no_data_found then 
    dbms_output.put_line('没有对应的员工信息');
    end;


   declare 
  v_ename varchar2(50);
   begin 
    select ename into v_ename from emp where empno=&eno;
   dbms_output.put_line(concat('员工名称:',v_ename));
   exception 
   when no_data_found then 
    dbms_output.put_line('没有对应的员工信息');
    end;






    PL/SQL程序块


    declare  ---定义常量  名称 constant 数据类型 赋值操作符 :=
             -- 定义变量 名称 数据类型 赋值 :=


    begin 
 


    exception  -- no_data_find  




    end;     --输出打印dbms_output.put_line();
    字符串拼接: || 或使用concat()函数


    /   -- command Window 执行plsql程序




declare 
    v_pi constant number(6,5):=3.14; --圆周率常量  
    v_r number(1):=2; --半径变量
    v_area number(10,2); --圆面积
    begin 
    v_area:=v_pi*v_r*v_r; --计算圆面积
    dbms_output.put_line('圆的面积:='||v_area);
    end;




%TYPE数据类型  --定义一个变量与表的列的数据类型一致
 
 --根据员工编号查找员工姓名和员工部门编号
declare
    v_ename emp.ename%type;
    v_deptno dept.deptno%type;
    begin
   select ename,deptno into v_ename,v_deptno from emp where empno=7369;
    dbms_output.put_line('员工名称:'||v_ename);
    dbms_output.put_line('员工部门编号:'||v_deptno);
    end;


--除了可以使用常量来给变量赋值之外(:=),还可通过SELECT INTO语句将从数据库表中查询的结果赋予变量 
当由多个变量通过select 赋值时 :selcet 列名1,列名2 into 变量1,变量2 from 表


%record 记录数据类型
首先需要定义记录类型和记录变量
当引用记录成员时,必须将记录变量作为前缀


语法结构 :
   type 记录类型名称 is record(
    列名1 数据类型,
    列名2 数据类型,
    列名3 数据类型
     );
   v_emp_record 记录类型名称;
   


--根据输入的员工编号输出该员工的姓名、基本工资、奖金及实发工资。


declare 
    type emp_record is record(
    v_empname emp.ename%type,
    v_sal emp.sal%type,
    v_deptno emp.deptno%type,
    v_deptname dept.dname%type
    );
    v_emp_record emp_record;
    begin 
    select ename,sal,emp.deptno,dname into v_emp_record from emp,dept where emp.deptno=dept.deptno and empno=&eno;
   dbms_output.put_line('员工姓名:'||v_emp_record.v_empname);
   dbms_output.put_line('员工薪资:'||v_emp_record.v_sal);
   dbms_output.put_line('部门编号:'||v_emp_record.v_deptno);
   dbms_output.put_line('部门名称:'||v_emp_record.v_deptname);
   exception 
   when no_data_found then 
   dbms_output.put_line('没有找到对应的员工信息!');
   end;


-- 简写 直接使用某个表的行记录  表名%rowtype




--TABLE数据类型
语法结构 type table数据类型名称 is table of 表名%rowtype index by binary_integer;
table就是一个一维数组 
赋值 select * into table(0) from 表 where id=




declare 
    type dept_table is table of dept%rowtype index by binary_integer; 
    v_dept_table dept_table;
    begin 
    select * into v_dept_table(0) from dept where deptno=&dno;
    select * into v_dept_table(1) from dept where deptno=&dno2;
    dbms_output.put_line('第一条记录信息:'||v_dept_table(0).deptno||v_dept_table(0).dname);
    dbms_output.put_line('第二条记录信息:'||v_dept_table(1).deptno||v_dept_table(1).dname);
   end;


   when no_data_found then 


条件控制语句:
语法结构


if condition1 then 
...
elsif condition2 then 
...
elsif condition3 then 
...
else 
...
end if;


-- 
输入员工编号,如果该员工
原来没有奖金,则按照工资的10%发放 
原来有奖金但不超过1000的,补到1000;
其余的按照原来奖金基础再加上10%发放;


declare 
emp_row emp%rowtype;
begin 
select * into emp_row from emp where empno=&eno;
dbms_output.put_line('员工的奖金:'||emp_row.comm);
if emp_row.comm is null then 
update emp set comm=sal*0.1 where empno=emp_row.empno;
elsif emp_row.comm<1000 then 
update emp set comm=1000 where empno=emp_row.empno;
else 
update emp set comm=emp_row.comm*1.1 where empno=emp_row.empno;
end if;
exception 
when no_data_found then
dbms_output.put_line('没有找到对应的员工信息');
end;






-- 条件控制语句 


case 表达式 


when 值1 then
......
when 值2 then
......
when 值3 then 
......
else 
.....
end case;


--根据部门编号输出部门所在地


declare 
v_deptno dept.deptno%type:=&deptno;
begin 
case v_deptno 
when 10 then 
dbms_output.put_line('高新区');
when 20 then 
dbms_output.put_line('金水区');
when 30 then 
dbms_output.put_line('郑东新区');
when 40 then 
dbms_output.put_line('二七区');
else 
dbms_output.put_line('非郑州区域');
end case;
end;




declare 
v_deptname dept.dname%type:=&deptname;
begin 
case v_deptname 
when '高新区' then 
dbms_output.put_line('10');
when '金水区' then 
dbms_output.put_line('20');
when '郑东新区' then 
dbms_output.put_line('30');
when '二七区' then 
dbms_output.put_line('40');
else 
dbms_output.put_line('非郑州区域');
end case;
end;




declare 
emp_row emp%rowtype;
begin 
select * into emp_row from emp where empno=&eno;
dbms_output.put_line('员工的奖金:'||emp_row.comm);








-- 循环语句语法结构
loop
退出条件:
if condition then exit; end if;
执行循环语句;
循环条件变量的改变;
end loop;




--定义一个dept类型的表结构
手工添加3条数据


然后用循环将其数据添加到dept表中。
declare 
type dept_table is table of dept%rowtype index by binary_integer;
v_dept_table dept_table;
i number(1):=0;
begin 
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='Java开发部';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='C++开发部';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='.NET开发部';
v_dept_table(3).deptno:=80;
v_dept_table(3).dname:='PHP开发部';
loop 
if i>2 then exit; end if; -- 退出条件 
insert into dept(deptno,dname) values(v_dept_table(i).deptno,v_dept_table(i).dname);  --循环语句
i:=i+1;   --变量改变
end loop;
end;


--while 循环


declare 
type dept_table is table of dept%rowtype index by binary_integer;
v_dept_table dept_table;
i number(1):=0;
begin 
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='Java开发部';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='C++开发部';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='.NET开发部';
v_dept_table(3).deptno:=80;
v_dept_table(3).dname:='PHP开发部';
while i<3 loop  --循环执行条件
insert into dept(deptno,dname) values(v_dept_table(i).deptno,v_dept_table(i).dname);  --循环语句
i:=i+1;   --变量改变
end loop;
end;


-- for循环


FOR 循环变量 in [REVERSE] 初值表达式..终值表达式 LOOP
语句段;
END LOOP;




declare 
type dept_table is table of dept%rowtype index by binary_integer;
v_dept_table dept_table;
begin 
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='Java开发部';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='C++开发部';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='.NET开发部';
v_dept_table(3).deptno:=80;
v_dept_table(3).dname:='PHP开发部';
for i in 0..2 loop  
insert into dept(deptno,dname) values(v_dept_table(i).deptno,v_dept_table(i).dname);  --循环语句
end loop;
end;


declare 
type dept_table is table of dept%rowtype index by binary_integer;
v_dept_table dept_table;
begin 
v_dept_table(0).deptno:=50;
v_dept_table(0).dname:='Java开发部';
v_dept_table(1).deptno:=60;
v_dept_table(1).dname:='C++开发部';
v_dept_table(2).deptno:=70;
v_dept_table(2).dname:='.NET开发部';
v_dept_table(3).deptno:=80;
v_dept_table(3).dname:='PHP开发部';
for i in reverse 0..3 loop  
dbms_output.put_line('i:='||i);
insert into dept(deptno,dname) values(v_dept_table(i).deptno,v_dept_table(i).dname);  --循环语句
end loop;
end;
0 0
原创粉丝点击