【安博培训笔记】Oracle5 使用 PL/SQL 作业20130911

Oracle5 使用 PL/SQL 作业
不熟练:
to_date('2001-09-11','yyyy-mm-dd')
select *  from dual;
dbms_output.put_line(empinfo.empno||empinfo.ename);


一、完成以下PL/SQL
1.  求半径2米,高3米的圆柱体体积。
declare
    tiji number;
begin
    tiji := 2*2*3.14*3;
    dbms_output.put_line('圆柱的体积为:'||tiji);
end;


declare
    r number:=2;
    h number:=3;
    p number:=3.14;
    area number;
begin
    area:=p*r*r*h;
    dbms_output.put_line('体积为:'||area);
end;


2.  求字符串‘abcdefg’的长度。
declare
    len number;
begin
    len := length('abcdefg');
    dbms_output.put_line('字符串abcdefg的长度为:'||len);
end; 


declare
   len number;
begin
    select length('abcdefg') into len from dual;
    dbms_output.put_line('字符串的长度为:'||len);
end;


@3.  显示pubs数据库中所有作家的au_lname的开头两个字母,大写显示。(字符函数) 
use pubs go select upper(left(au_lname,2)) from authors;


declare
    cursor cur is select au_lname from pubs;
    str pubs.au_lname%type;
begin
    open cur;
    loop
    fetch cur into str;
    str:= upper(substr(str,1,2));
    dbms_output.put_line(str);
    exit when cur%notfound;
    end loop;
end;
4.  取字符串‘abcdefg’的第2到第5个字母。(字符函数)
declare
    v_string varchar2(10);
begin
    v_string := substr('abcdefg',2,4);
    dbms_output.put_line(v_string);
end;


declare
   otherStr varchar2(4);
begin
   select substr('abcdefg',2,4) into otherStr from dual;
   dbms_output.put_line(otherStr);
end;


5.  计算2001年9月11日到今天已经多少天了。(日期函数) 
declare 
    day number;
begin
    day := trunc(sysdate - to_date('2001-09-11','yyyy-mm-dd'));
    dbms_output.put_line(day);
end;


declare
   days number;
begin
   select trunc(sysdate-to_date('2001-09-11','yyyy-mm-dd')) into days from dual;
   dbms_output.put_line('距离2001年9月11日到今天'||days||'天');
end;


6.  查询emp表,找出编号为7395的记录,如果名字为SCOTT 显示“找到了”否则显示“没找到”。(if语句)
declare
  scottinfo emp%rowtype;
begin
  select * into scottinfo from emp where empno=7395;
  if(scottinfo.ename = 'SCOTT') then    
     dbms_output.put_line('找到了');
    else
     dbms_output.put_line('没找到');
   end if;
exception
  when no_data_found then
    dbms_output.put_line('没有找到员工');
end;
@7.  编写一个程序,用以接受用户输入的数字。将该数左右反转,然后显示反转后的数
    提示:使用循环控制结构.
declare
  shuzi varchar2(20);
  len   number;
begin
  shuzi := &shuzi;
  len   := length(shuzi);
  for i in reverse 1 .. len loop
    dbms_output.put(substr(shuzi, i, 1));
  end loop;
  dbms_output.put_line('');
end;


declare
  v_char1 varchar2(100);
  v_char2 varchar2(100);
  v_c char(1); 
begin
  v_char1 :='&no';
  dbms_output.put_line('输入的数据为:'||v_char1);
  for i in reverse 1..length(v_char1)
  loop   
      v_c := substr(v_char1,i,1);    
      v_char2 := v_char2 ||v_c;
  end loop;
  dbms_output.put_line('反转后的数字为:'||v_char2);
end;


8.  编写一个程序,在emp表中根据empno查询职员信息。如果代码引发NO_DATA_FOUND异常,则显示一则消息.
select * from emp where empno = &empno;
declare
  empinfo emp%rowtype;
begin
  select * into empinfo from emp where empno = '&empno';
  dbms_output.put_line(empinfo.empno||empinfo.ename);
exception
  when no_data_found then
    dbms_output.put_line('没有该职员');
end;


declare
  v_emp   emp%rowtype;
  v_empno emp.empno%type;
begin
  select * into v_emp from emp where empno = &v_empno;
  dbms_output.put_line('编号:' || v_emp.empno || '  姓名:' || v_emp.ename ||
                       '     职位:' || v_emp.job || '    上级领导编号:' ||
                       v_emp.mgr || '     雇佣日期:' || v_emp.hiredate ||
                       '      工资:' || v_emp.sal || '     奖金:' ||
                       v_emp.comm || '  部门:' || v_emp.deptno);
exception
  when no_data_found then
    dbms_output.put_line('没找到此员工');
end;   
       
9.  编写一个程序,用以接受用户输入的DEPTCODE,并从employee表中检索该雇员的EMPNO。如果代码引发TOO_MANY_ROWS异常,则显示消息“返回多行”.
select * from emp;


declare
   scottinfo emp.empno%type;
begin
   select empno into scottinfo from emp  where deptno = '&deptno';
exception
    when too_many_rows then
       dbms_output.put_line('返回多行');
end;


declare
   scottinfo employee.empno%type;
begin
   select empno into scottinfo from employee  where deptcode = '&deptcode';
exception
    when too_many_rows then
       dbms_output.put_line('返回多行');
end;


declare
  v_empno    emp.empno%type;
  v_deptno   emp.deptno%type;
begin
  select empno into v_empno from emp where deptno = &v_deptno;
  dbms_output.put_line(v_empno);
exception
  when too_many_rows then
    dbms_output.put_line('返回多行');
end;


declare
  v_empno    employee.empno%type;
  v_deptcode employee.detcode%type;
begin
  select empno into v_empno from employee where deptcode = &v_deptcode;
  dbms_output.put_line(v_empno);
exception
  when too_many_rows then
    dbms_output.put_line('返回多行');
end;


关于dual
1. dual 确实是一张表.是一张只有一个字段,一行记录的表. 
2.习惯上,我们称之为'伪表'.因为他不存储主题数据.
3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.
如:select * from mytable ;
select * from myview;
等等.
但如果我们不需要从具体的表来取得表中数据,而是单纯地为了得到一些我们想得到的信息,并要通过select 完成时,就要借助一个对象,这个对象,就是dual;
如我们要计算 999*999 的值,可以用:
select 999*999 from dual;
来实现;
要拼接一个电话信息:
select concat('010-','88888888')||'转23' 高乾竞电话 from dual;
就变成了我们想要的格式输出.
4.当然,我们不一定要dual ,也可以这样做.例如:
create table mydual( dummy varchar2(1));
也可以实现和dual 同样的效果:
select 999*999 from mydual;
不过,dual 我们都用习惯了,就无谓自己再搞一套了.
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值