您的位置:首页 > 数据库 > Oracle

Oracle-PL/SQL

2015-11-24 13:55 344 查看
实验1:写一个PL/SQL,输出”Hello World”。

begin
dbms_output.put_line('Hello World');
end;


实验2:写一个PL/SQL,查询emp表的工资,输入员工编号,根据编号查询工资,如果工资高于3000无,则显示高工资,如果工资大于2000元,则显示中等工资,如果工资小于2000元,则显示低工资。

declare
v_sal emp.sal%type;
v_empno emp.empno%type;
begin
v_empno := &empno_Value;
select sal into v_sal from emp where empno = v_empno;
if v_sal > 3000 then
dbms_output.put_line('高工资='||v_sal);
elsif v_sal > 2000 then
dbms_output.put_line('中等工资'||v_sal);
else
dbms_output.put_line('低工资'||v_sal);
end if;
end;
/


实验3:自已学习实验FOR循环,尝试写一个PL/SQL,用FOR循环向一个表中自动插入20行记录。 比如:创建一个简单的表testtable,这个表只有两列(id,currentdate),id为number类型,currentdate为date类型。然后用PL/SQL向这个表插入20行。 思路:先写一个最简单的FOR循环PL/SQL块,再写一个最简单的插入记录PL/SQL块,上面两个块都能执行成功,最后进行结合就可以了吧。

declare
v_row testtable%rowtype;
v_date testtable.currentdate%type;
begin
for i in 0..20 loop
insert into testtable values(i,sysdate);   //可以直接使用sysdate函数获取当前日期
select * into v_row from testtable where id=i;
dbms_output.put_line(v_row.id||'   '||v_row.currentdate);
end loop;
end;
declare
v_row testtable%rowtype;
v_date testtable.currentdate%type;
begin
select sysdate into v_date from dual; //从虚表中通过sysdate函数获取当前日期
for i in 0..20 loop
insert into testtable values(i,v_date);
select * into v_row from testtable where id=i;
dbms_output.put_line(v_row.id||'   '||v_row.currentdate);
end loop;
end;


选做实验: 实验4:用户输入一个员工编号,根据它所在部门给上涨工资,规则: 10部门上涨10%,20上涨20%,30上涨30% 要求最高不能超过5000元,超过5000元就停留在5000元。 提示: (1)需要声明几个变量?

1.工资金额 2.员工编号 3.员工部门 (2)if语句可以嵌套,可以实验一个简单的嵌套先体会一下,然后再写这个题目的要求。

declare
v_empno emp.empno%type;
v_deptno emp.deptno%type;
v_sal emp.sal%type;
begin
v_empno := &员工编号;
select deptno,sal into v_deptno,v_sal from emp where empno = v_empno;
if v_deptno = 10 then
dbms_output.put_line('old_sal='||v_sal);
v_sal := v_sal*1.1;
if v_sal > 5000 then
v_sal := 5000;
end if;
update emp set sal =v_sal;
dbms_output.put_line('new_sal='||v_sal);
elsif v_deptno = 20 then
dbms_output.put_line('old_sal='||v_sal);
v_sal := v_sal*1.2;
if v_sal > 5000 then
v_sal := 5000;
end if;
update emp set sal =v_sal;
dbms_output.put_line('new_sal='||v_sal);
elsif v_deptno = 30 then
dbms_output.put_line('old_sal='||v_sal);
v_sal := v_sal*1.3;
if v_sal > 5000 then
v_sal := 5000;
end if;
update emp set sal =v_sal;
dbms_output.put_line('new_sal='||v_sal);
else
dbms_output.put_line('其他数据不增加');
end if;
commit;
end;
/


实验注意

1.关于sysdate()函数的使用

2.关于if嵌套结构的注意

实验总结:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  plsql oracle