begin dbms_output.put_line('这是外边的块'); begin dbms_output.put_line('这是里边的块'); end; end;
begin dbms_output.put('这是外边的块'); begin dbms_output.put_line('这是里边的块'); end; end;
begin begin dbms_output.put('这是里边的块'); end; dbms_output.put_line('这是外边的块'); end;
begin begin dbms_output.put_line('111111'); begin dbms_output.put_line('222222'); end; begin dbms_output.put_line('333333'); end; end; dbms_output.put_line('444444'); end; ----按照 1 2 3 4 的顺序打印
declare v_xuehao number(2) :=1; begin declare v_chengji number(4,1):=95; begin dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。'); end; dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。');--报错,识别不到v_chengji end;
declare v_xuehao number(2) :=1; v_chengji number(4,1):=95; begin declare
begin dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。'); end; dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。'); end;
begin declare v_xuehao number(2) :=1; begin dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。'); end; declare v_chengji number(4,1):=95; begin dbms_output.put_line('小明的学号为:'||v_xuehao||',成绩为:'||v_chengji||'。'); end; end;
declare v_no number :=&学号; v_name varchar2(20):=’&姓名’;–使用弹窗给字符串赋值时,’&弹窗提示名’ 需要用单引号括起来 v_class number :=&班级; v_date date := to_date(‘&入学日期’,’yyyy/mm/dd’); begin dbms_output.put_line(‘学号为:’||v_no||’,姓名为:’||v_name||’,班级为:’||v_class||’,入学日期为:’||v_date||’。’); end;
弹窗赋值使用方法二
declare v_no number :=&学号; v_name varchar2(20):=&姓名;–在弹窗内输入值时,字符串类型需要单引号括起来 v_class number :=&班级; v_date date :=&入学日期;–在弹窗内输出日期时,需要使用转换函数把日期转为date类型 begin dbms_output.put_line(‘学号为:’||v_no||’,姓名为:’||v_name||’,班级为:’||v_class||’,入学日期为:’||v_date||’。’); end;
思考题: 1.在块中能不能直接使用 select 查询信息,比如查询员工’SMITH’的薪资–不能,会报错 select sal from emp where ename = ‘SMITH’;
declare v_sal number; begin select sal into v_sal from emp where ename = ‘SMITH’;–可以通过select into 赋值的方法在块中使用select语句 dbms_output.put_line(‘史密斯的薪资为:’||v_sal); end;
2.打印出emp表中 员工编号为7369的员工姓名与所在部门 declare v_ename varchar2(20); v_deptno number; begin select ename,deptno into v_ename,v_deptno from emp where empno=7369; dbms_output.put_line(‘员工编号为7369的员工姓名’||v_ename||’与所在部门’||v_deptno); end;
3.打印出emp表中 员工编号为7369的员工姓名与部门所在地 declare v_ename varchar2(20); v_loc varchar2(20); begin select ename, loc into v_ename, v_loc from emp a join dept b on a.deptno = b.deptno where empno = 7369; dbms_output.put_line(‘员工姓名为’ || v_ename || ‘部门所在地是’ || v_loc); end;
声明变量(日期型),当手动输入一个日期后,打印出这是本年的第几周 格式为’XX在本年的第Y周’
1 2 3 4 5
declare v_date date :=to_date('&日期','yyyy-mm-dd'); begin dbms_output.put_line(v_date||'在本年的第'||to_char(v_date,'ww')||'周'); end;
打印出emp表中 薪资为5000 的员工姓名、薪资与部门名称
1 2 3 4 5 6 7 8 9 10
declare v_ename varchar2(7); v_sal number(5):=5000; v_dname varchar2(10); begin select ename,sal,dname into v_ename,v_sal,v_dname from emp a join dept b on a.deptno=b.deptno where sal=5000; dbms_output.put_line(v_ename||' '||v_sal||' '||v_dname); end;
select ename,sal,dname from emp a join dept b on a.deptno=b.deptno and sal=5000;--on 跟 where 的区别
declare v_deptno number:=&部门编号; v_ename varchar2(10); begin select ename into v_ename from (select ename from emp where sal in (select min(sal) from emp where deptno = v_deptno) and deptno = v_deptno) where rownum =1; dbms_output.put_line('部门编号'||v_deptno||'员工姓名'||v_ename); end;
declare v_deptno number:=&部门编号; v_ename varchar2(10); begin select ename into v_ename from (select ename from emp where sal = 3000 and deptno = v_deptno) where rownum =1; dbms_output.put_line('部门编号'||v_deptno||'员工姓名'||v_ename); end;
create table emp_copy as select * from emp; create table emp_copy1 as select * from emp_copy; select * from v_emp_copy; select * from emp_copy1; select * from emp_copy;
--创建表用于备份数据 create table v_emp_copy as select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp_copy group by empno,ename,job,mgr,hiredate,sal,comm,deptno having count(1)>1; select * from v_emp_copy;
--删除数据 delete from emp_copy where empno in (select empno from (select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(1) from emp_copy group by empno,ename,job,mgr,hiredate,sal,comm,deptno having count(1)>1) group by empno); select * from emp_copy for update;
--重新插入重复数据 insert into emp_copy select * from v_emp_copy; select * from emp_copy; select * from emp;
--删除表释放空间 drop table v_emp_copy drop view v_emp_copy
例题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
declare v_deptno number:=&部门编号; v_xingming varchar2(10); begin select ename into v_xingming from emp where (sal,deptno) in(select min(sal),deptno from emp where deptno=v_deptno group by deptno); dbms_output.put_line('部门编号'||v_deptno||'员工姓名'||v_xingming); end;
select ename from emp where (sal,deptno) in(select min(sal),deptno from emp where deptno=10 group by deptno);
declare v_sal number; begin select sal into v_sal from emp where sal = 3000;--报错,实际返回的行数多于一行 dbms_output.put_line(v_sal); end;
打印工资第5名的员工姓名
1 2 3 4 5 6 7 8
declare v_ename varchar2(10); begin select ename into v_ename from (select ename,rownum r from (select * from emp order by sal desc))where r between 5 and 5; dbms_output.put_line('姓名:'||v_ename); end;
select ename into v_ename from (select ename,rownum paixu from (select * from emp order by sal desc)) where paixu = 5;
打印出KING的员工编号和入职时间(精确到天)和对应的部门名称
1 2 3 4 5 6 7 8
declare v_empno number; v_date date; v_dname varchar2(10); begin select empno,hiredate,dname into v_empno,v_date,v_dname from emp A join dept B on A.deptno = B.deptno and A.ename = 'KING'; dbms_output.put_line('员工编号:'||v_empno||',入职时间:'||v_date||',部门名称:'||v_dname); end;