Oracle

来源:互联网 发布:淘宝返利最高返多少 编辑:程序博客网 时间:2024/05/17 01:20
--自定义
--记录类型

type 名字 is record(
a number,
b varchar2(6)
)

表类型


type 名字 is table of 元素类型


index by binary_integer
-------
declare
type emp_name_table is table of emp.ename%type
index by binary_integer;
e emp_name_table;
begin
select ename BULK COLLECT  into e from emp;
dbms_output.put_line('元素个数:'||e.count);
dbms_output.put_line('第一个名字:'||e(1));
end;


select * from emp;


--赋值语句:
--select into
--select BULK COLLECT into e from emp;


--查询所有的员工信息,存储到一个表类型的变量中

--输出第一个元素的编号,姓名,职位,工资(行记录类型emp%rowtype5)
declare
type emp_name_table is table of emp%rowtype
index by binary_integer;
e emp_name_table;
begin
select * BULK COLLECT  into e from emp;
dbms_output.put_line('元素个数:'||e.count);
dbms_output.put_line('编号:'||e(1).empno);
dbms_output.put_line('姓名:'||e(1).empname);
dbms_output.put_line('职位:'||e(1).job);
dbms_output.put_line('工资:'||e(1).sal);
end;
 
--参照类型
--RETURVING语句:DML操作返回值赋给变量
--更新某个员工的工资,输出这个员工的姓名,新工资
declare
v_sal emp.sal%type :=500;
v_empno emp.empno%type :=7369;
v_name emp.ename%type;
v_new_sal emp.sal%type;
begin
update emp set sal=sal+v_sal where empno = v_empno;
--commit --提交事务,默认是不自动提交事务的
select ename,sal into v_name,v_new_sal from emp where empno = v_empno;
dbms_output.put_line('姓名:'+||v_name);
dbms_output.put_line('新工资:'+||v_new_sal);
end;




--------
declare
v_sal emp.sal%type :=500;
v_empno emp.empno%type :=7369;
v_name emp.ename%type;
v_new_sal emp.sal%type;
begin
--方法1,注意后面没有分号
update emp set sal=sal+v_sal where empno = v_empno
return ename,sal into v_name,v_new_sal;
--commit --提交事务,默认是不自动提交事务的
--方法2 select into
--select ename,sal into v_name,v_new_sal from emp where empno = v_empno;
dbms_output.put_line('姓名:'+||v_name);
dbms_output.put_line('新工资:'+||v_new_sal);
end;




--更新20号部门的所有员工的工资(工资+100),并返回结果
declare
v_sal emp.sal%type :=500;
v_empno emp.empno%type :=7369;
type emp_type_record is record(
v_name emp.ename%type,
v_new_sal emp.sal%type
);
type emp_table_type is table of emp_type_record
index by binary_integer;
e emp_table_type;

begin
--方法1,注意后面没有分号
update emp set sal=sal+v_sal where deotno = 20
return ename,sal bulk collect into e;
--commit --提交事务,默认是不自动提交事务的
--方法2 select into
--select ename,sal into v_name,v_new_sal from emp where empno = v_empno;
dbms_output.put_line('总更新了多少:'+||e.count);
dbms_output.put_line('姓名:'+||e(1).v_name);
dbms_output.put_line('新工资:'+||e(1).v_new_sal;
end;




--select赋值语句
--单条记录的赋值:
select 列1 into 变量 1 ,from 表


--多条记录的赋值
select 列1,列2.. BULK COLLECT into 表类型 from 表类型


--dml返回值




------------------------------
--条件分支判断
--最简单的if else 
declare
i number :='&input';
begin
--if i=null then
if i is null then
dbms_output.put_line('请输入一个数字');
return ;
end if;
if i>10 then
dbms_output.put_line(i||'>10');
else
dbms_output.put_line(i||'<10');
end if;

end;


--特殊比较运算符
declare
i number :='&input';
begin
--if i=null then
if i is null then
dbms_output.put_line('请输入一个数字');
return ;
end if;
if i between 10 and 20 then
dbms_output.put_line(i||'在10和20之间');
else
dbms_output.put_line(i||'不在10和20之间');
end if;

end;


--输入一个分数,判断等级 if else
--100没有这个分数
--90-100学霸,70-80优秀,60-70及格
declare
i number :='&input';
begin
--if i=null then
if i is null then
dbms_output.put_line('请输入一个数字');
return ;
end if;
if i>100 then
dbms_output.put_line('没有这么高的分数');
elsif i>=90 then
dbms_output.put_line('学霸');
elsif i>=80 then
dbms_output.put_line('优秀');
elsif i>=70 then
dbms_output.put_line('良好');
elsif i>=60 then
dbms_output.put_line('及格');
else then
dbms_output.put_line('继续努力');
end if;

end;




--输入员工编号,判断该员工的工资等级
--8000以上A,8000-6000 B ,4000-6000 C,其他是E
--输出员工编号,姓名,工资,工资等级
--编号不存在,提示查无此人
declare
i number :='&input';
--参照数据类型
--v_empno emp.empno%type :=&input;
v_empno number :=&input;
v_level varchar2(1);
--定义结构类型
type emp_type_record is record(
--成员变量
v_no emp.empno%type,
v_ename emp.ename%type,
v_sal emp.sal%type,
v_count binary_integer;
);
--引用结构类型
e emp_type_record;
begin
select count(1) into from emp where empno = v_empno;
if v_count=0 then
dbms_output.put_line('查无此人,请重新输入');
return; --找不到记录,直接返回
end if;

select empno,ename,sal into e from emp where empno =v_empno;
if e.v_sal is null then
dbms_output.put_line('请输入员工编号:');
return ;
end if;

if e.v_sal > 8000 then
v_level :='A';
elsif e.v_sal > 6000 then
v_level :='B';
elsif e.v_sal > 4000 then
v_level :='C';
elsif e.v_sal > 3000 then
v_level :='D';
else then
v_level :='E';
end if;
dbms_output.put_line('编号:'||e.v_no||'姓名:'||e.v_ename||'工资:'||e.v_sal||'等级:'||'v_level');

end;




--case when 语句
begin
v_txt := case v_c
when then 
when then 
end;
end;
--嵌入式
--单独使用
--输入一个字符判断
--A 优秀
--B 良好


declare 
v_c char(1) :='&input';
v_txt char(4);
begin
v_txt := case v_c
when 'A' then '优秀'
when 'B' then '良好'
when 'C' then '及格'
ELSE '差劲'
end;
dbms_output.put_line('级别:'||v_txt);
end;


--单独使用
declare 
v_c char(1) :='&input';
v_txt char(4);
begin
case v_c
when 'A' then 
v_txt := '优秀';
when 'B' then 
v_txt := '良好';
when 'C' then 
v_txt := '及格';
ELSE 
v_txt := '差劲';
end case;
dbms_output.put_line('级别:'||v_txt);
end;




--循环,基本循环


declare 
i binary_integer :=0;
begin
loop
i :=i+1;
if i >10 then 
exit; --退出循环
end if;
dbms_output.put_line('hello world');

end loop;
end;


declare 
i binary_integer :=0;
begin
loop
i :=i+1;
exit when i >10; --退出循环
dbms_output.put_line('hello world');

end loop;
end;


declare 
i binary_integer :=0;
begin
while i<10 loop
i :=i+1;
dbms_output.put_line('hello world');
end loop;
end;


declare 
i binary_integer :=0;
begin
while true loop
i :=i+1;
exit when i >10; --退出循环
dbms_output.put_line('hello world');
end loop;
end;


--for
--递增
declare
begin
for i in 1.. 10 loop
dbms_output.put_line('hello world'||i);
end loop;
end;


--倒序显示数字,递减,步增1
declare


begin
for i in reverse 1.. 10 loop
dbms_output.put_line('hello world'||i);
end loop;
end;


--循环输出雇员表的信息,按照以下格式输出
--编号:xx
--姓名:xx
--入职日期:xx
--部门编号:xx


declare
type emp_table_record is table of emp%rowtype;
e emp_table_record;

begin
select * bulk collect into e from emp;
for i in 1.. e.count loop
dbms_output.put_line(i||')-----------');
dbms_output.put_line('编号:'||e(i).empno);
dbms_output.put_line('姓名:'||e(i).ename);
dbms_output.put_line('职位:'||e(i).job);
dbms_output.put_line('入职日期:'||to_char(e(i).hiredate,'yyyy-mm-dd');
end loop;
end;




--总结:
0 0
原创粉丝点击