📄 郑国@6月8日.txt
字号:
1,定义一个函数,输入一个大于0的整数n, 返回1到n的整数和
定义函数
create or replace function add_sum (n int)
return int
as
i int;
j int;
begin
j:=0;
i:=1;
while i<n loop
j:=j+i;
i:=i+1;
end loop;
return j;
end;
使用函数
declare
a int;
b int;
begin
a:=&输入一个数;
b:=add_sum(a);
dbms_output.put_line(b);
end;
==================================================================
定义一个函数,输入emp的empno,返回该行的sal
create or replace function emp_get_sal (eno emp.empno%type)
return emp.sal%type
as
salary emp.sal%type;
begin
select sal into salary from emp where empno=eno;
return salary;
end;
==========================================================================
定义一个更新emp表sal的存储过程,输入参数为empno,sal,根据empno更新相应的sal
create or replace procedure update_sal (eno emp.empno%type)
as
salary emp.empno%type;
begin
select sal into salary from emp where empno=eno;
if salary<2500 then
update emp set sal=3600 where empno=eno;
end if;
end;
==========================================================================
定义一个table 里面存放的是%rowtype类型,用该类型定义一个变量datas,从emp表取五行记录放到datas里面进行循环,循环里完成以下操作:先通过刚才定义的函数取sal,如果sal<2500 ,调用刚才写的存储过程更新sal为3600
create table temp as (select * from emp) on commit preserve rows; --建立一个临时表
declare
type mydata is table of emp.empno%type index by binary_integer;
type myrow is table of emp%rowtype index by binary_integer;
empno_data mydata;
row_data myrow;
i int:=1;
j int;
empno_min emp.empno%type;
empno_max emp.empno%type;
begin
select min(empno) into empno_min from emp;
select max(empno) into empno_max from emp;
--dbms_output.put_line(empno_min);
while empno_min<empno_max loop --当最小的员工号和最大的员工号相等时
--退出循环
select max(empno) into empno_max from temp;
select max(empno) into empno_data(i) from temp; --取得员工编号信息
update_sal(empno_data(i));
/*使用了update_sal 过程如果 sal<2500 ,调用刚才写的存储过程更新sal为3600*/
select * into row_data(i) from temp where empno=empno_data(i);
delete from temp where empno=empno_max; --删除临时表里最大的员工号
dbms_output.put(empno_data(i));
dbms_output.put_line(' '||i);
i:=i+1;
end loop;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -