plsql的使用

plsql的使用

1).每次运行plsql,都必须运行的语句,否则没有结果?

set serveroutput on

2).plsql都有那些标识符?

变量:V_name,常量:C_name,异常标识:E_name
游标变量:name_cursor,记录类型:name_record

3).plsql 的语法规则?

–declare
–声明的变量、类型、游标
begin
dbms_output.put_line(‘helloworld’);
–exception
–针对begin块中出现的异常,提供处理的机制
end;

4).plsql 中查询的操作?

select…into…from…where…
eg:select salary,email, into v_sal,v_email from employees

5).创建记录类型的两种方式和声明记录类型?

1.第一种:
type emp_record is record(
v_sal employees.salary%type,
v_email employees.email%type,
v_hire_date employees.hire_date%type,
);
v_emp_record emp_record;

第二种:
type salary_record is record(
v_name varchar(20),
v_salary number(10,2)
);
v_salary_record salary_record;

第三种:
v_emp_record employees%rowtype

6).在 begin 中使用记录类型的变量?

v_salary_record.v_name := ‘刘德华’;
v_salary_record.v_salary := 120000;

7).两种条件语句的使用?

1.IF语句:
If…then…;
elsif…then…;
else…;
end if;

2.CASE表达式
case…when…then…when…then…else…end;

v_temp := case trunc(v_salary/5000) when 0 then ‘salary<5000’…

v_temp := case ‘v_job_id’ when ‘IT_PROG’ then ‘GRADE: A’…

8).两种循环语句的使用?

第一种:WHILE循环:while…loop…;end loop;
while v_i <= 100 loop
dbms_output.put_line(v_i);
v_i := v_i +1;
end loop;

第二种:FOR循环:for…in[下限]…[上限]loop…;end loop;
for c in (reverse反转)1…100 loop
dbms_output.put_line©;
end loop;

9).输出2-100之间的质数?

1.用 WHILE 循环:
declare
v_i number(3) := 2;
v_j number(3) := 2;
v_flag number(2);
begin
while v_i<=100 loop

  while v_j <= (v_i/2) loop
       if mod(v_i,v_j) = 0 then v_flag := 0;end if;
       v_j := v_j + 1;
  end loop;
  if v_flag = 1 then dbms_output.put_line(v_i);end if;
  
  v_j := 2;
  v_i := v_i + 1;
  v_flag := 1;

end loop;
end;

2.用 FOR 循环:
declare
v_flag number(2) := 1;
begin
for v_i in 2…100 loop

 for v_j in 2..sqrt(v_i) loop
 if mod(v_i,v_j) = 0 then v_flag := 0; end if;
 end loop;
 
 if v_flag = 1 then dbms_output.put_line(v_i); end if;
 v_flag := 1;
 end loop;

end;

10).打印1-100的自然数,当打印到50时,跳出循环,输出"打印结束"?

第一种方法:
begin
for v_i in 1…100 loop
if v_i = 50 then goto label;
end if;

 dbms_output.put_line(v_i);

end loop;
<

第二种方法
begin
for v_i in 1…100 loop
if v_i = 50 then dbms_output.put_line(‘打印结束’);
exit;
end if;

 dbms_output.put_line(v_i);

end loop;
end;

11).什么时候使用游标?

对于处理多行记录的事务经常使用游标来实现

12).游标常用的属性?

%FOUND 布尔型属性,当最近一次读记录时成功返回,则值为 TRUE;
%NOTFOUND 布尔型属性,与%FOUND 相反;
%ISOPEN 布尔型属性,当游标已打开时返回 TRUE;
%ROWCOUNT 数字型属性,返回已从游标中读取的记录数。

13).使用 WHILE 循环处理游标的步骤?

打印出 80 部门的所有的员工的 employee_id 和 salary :
declare
v_sal employees.salary%type;
v_empid employees.employee_id%type;
–定义游标
cursor emp_sal_cursor is select salary,employee_id from employees where department_id = 80;
begin
–打开游标
open emp_sal_cursor;
–提取游标
fetch emp_sal_cursor into v_sal,v_empid;
while emp_sal_cursor%found loop
dbms_output.put_line(‘employee_id:’||v_empid||’,salary:’||v_sal);
fetch emp_sal_cursor into v_sal,v_empid;
end loop;
–关闭游标
close emp_sal_cursor;
end;

14).使用 FOR 循环处理游标的步骤?
更新所有员工的工资:工资不大于5K,提高5%,工资不大于10K,提高3%;其它提高1%。
declare
v_temp number(4,2);
cursor emp_sal_cursor is select salary,employee_id from employees;
begin
for c in emp_sal_cursor loop
if c.salary <= 5000 then v_temp := 0.05;
elsif c.salary<=10000 then v_temp := 0.03;
elsif c.salary<=15000 then v_temp := 0.02;
else v_temp := 0.01;
end if;

   update employees set salary = salary *(1+v_temp) where employee_id = c.employee_id;

end loop;
end;

15).游标 FOR 循环语句的优点?

游标 FOR 循环语句,自动执行游标的 OPEN、 FETCH、 CLOSE 语句和循环语句的功能

16).隐式游标?
1.对于非查询语句, ORACLE 系统自动地为这些操作设置隐式游标, 隐式游标的名字为 SQL。
2.对于隐式游标的操作,如定义、打开、取值及关闭操作,都由 ORACLE 系统自动地完成,无需用户进行处理。
3.用户只能通过隐式游标的相关属性,来完成相应的操作。
4.隐式游标属性:
SQL%FOUND 布尔型属性,当最近一次读记录时成功返回,则值为 TRUE;
SQL%NOTFOUND 布尔型属性,与%FOUND 相反;
SQL%ROWCOUNT 数字型属性, 返回已从游标中读取得记录数;
SQL%ISOPEN 布尔型属性, 取值总是 FALSE。
5.SQL 命令执行完毕立即关闭隐式游标。

16).隐式游标的使用?
更新指定员工 salary(涨工资 10),如果该员工没有找到,则打印”查无此人” 信息
begin
update employees set salary = salary + 10 where employee_id = 1005;
if sql%notfound then dbms_output.put_line(‘查无此人!’); end if;
end;

17).三种异常处理的方式?
1.预定义的异常处理
2.非预定义的异常处理
3.用户自定义的异常处理

18).存储过程和触发器的区别:
存储过程,你调用的时候才会执行(手动)
触发器就是你设定了数据库里比如删除,修改,插入时,就会触发(自动)
触发器不能接收参数

19).定义一个存储函数: 获取给定部门的工资总和?

要求:部门号定义为参数, 工资总额定义为返回值.
create or replace function sum_salary(dept_id number)
return number
is
v_sum_salary number(15) := 0;
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
for c in salary_cursor loop
v_sum_salary := v_sum_salary +c.salary;
end loop;
return v_sum_salary;
end;

20).编写一个存储过程:获取给定部门的工资总和(通过 out 参数)?
要求:部门号和工资总额定义为参数
create or replace procedure sum_sal(dept_id number,sumsal out number)
is
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
sumsal := 0;
for c in salary_cursor loop
sumsal := sumsal +c.salary;
end loop;
dbms_output.put_line(sumsal);
end;
查询:
declare
v_sal number(10) := 0;
begin
sum_sal(80,v_sal);
end;

21).创建一个存储函数,返回当前的系统时间?
create or replace function get_time
return date
is
v_date date;
begin
v_date := sysdate;
return v_date;
end;

22).存储函数和存储过程的区别?
不同点:
1、标识符不同。函数的标识符为FUNCTION,过程为:PROCEDURE。
2、函数中有返回值,且必须返回,而过程没有返回值。
3、过程无返回值类型,不能将结果直接赋值给变量;函数有返回值类型,调用时,除在select中,必须将返回值赋给变量。
4、函数可以在select语句中直接使用,而过程不能,例如:假设已有函数fun_getAVG() 返回number类型绝对值。那么select fun_getAVG(col_a) from table 这样是可以的。
相同点:
二者都可以有出参

23).一个helloworld级别的触发器?
create or replace trigger update_emp_trigger2
after update on employees for each row
begin
dbms_output.put_line(‘old:salary’||:old.salary||’,’||‘new salary:’||:new.salary);
end;

24).编写一个触发器, 在对 my_emp 记录进行删除的时候, 在 my_emp_bak 表(新建的表)中备份对应的记录?
create or replace trigger delete_emp_trigger
before delete on my_emp for each row
begin
insert into my_emp_bak
values(:old.employee_id,:old.salary);
end;

25).触发器的组成?
触发事件:即在何种情况下触发 TRIGGER; 例如: INSERT, UPDATE, DELETE。
触发时间:即该 TRIGGER 是在触发事件发生之前( BEFORE)还是之后(AFTER)触发
触发器本身:即该 TRIGGER 被触发之后的目的和意图,正是触发器本身要做的事情
触发频率:STATEMENT只执行一次,ROW受影响的每行代码都执行一次

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值