【安博培训笔记】Oracle5 使用 PL/SQL 作业20130911
2013-09-11 21:37
344 查看
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 我们都用习惯了,就无谓自己再搞一套了.
不熟练:
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 我们都用习惯了,就无谓自己再搞一套了.
相关文章推荐
- 【安博培训技术】Oracle5 使用 PL/SQL20130911
- 【安博培训笔记】Oracle2 基本SQL查询 作业20130909
- 【安博培训笔记】Oracle6 游标管理 作业20130911
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- 【安博培训笔记】Oracle3 操作符及SQL函数 作业20130910
- 【安博培训笔记】Oracle7 子程序和程序包 作业20130914
- Oracle笔记 九、PL/SQL 游标的使用
- 【安博培训笔记】Oracle8 触发器 作业20130914
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- 【安博培训笔记】Oracle1 入门 作业20130909
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- 【安博培训笔记】Oracle4 事务和数据库对象 作业20130910
- [Oracle]高效的PL/SQL程序设计(一)--伪列ROWNUM使用技巧
- Oracle PL/SQL管理LDAP服务器(DBMS_LDAP包的使用)