sql;">
declare
v_sal number(10); (注意每句话后面别忘记了分号,跟java中的一样)
begin
select salary into v_sal from employees where employee_id = 100;
dbms_output.put_line(v_sal);
end;
举例2:
sql;">
declare
v_sal number(10); (注意,这里声明的空间大小不能比原表中的小)
v_email varchar2(20);
v_hire_date date;
begin
select salary,email,hire_date into v_sal,v_email,v_hire_date from employees where employee_id =
100;
dbms_output.put_line(v_sal||','||v_email||','||v_hire_date);
end;
或者:
declare
v_sal employees.salary%type;
v_email employees.email%type;
v_hire_date employees.hire_date%type;
begin
select salary,'||v_hire_date);
end;
sql;">
declare
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;
begin
select salary,hire_date into v_emp_record from employees where employee_id = 100;
dbms_output.put_line(v_emp_record.v_sal||','||v_emp_record.v_email||','||
v_emp_record.v_hire_date);
end;
sql;">
declare
v_job_id employees.job_id%type;
v_temp varchar2(20);
begin
select job_id into v_job_id from employees where employee_id =122;
v_temp :=
case v_job_id when 'IT_PROG' then 'A'
when 'AC_MGT' then 'B'
when 'AC_ACCOUNT' then 'C'
else 'D'
end;
dbms_output.put_line('job_id:'||v_job_id||' '||v_temp);
end;
使用循环语句打印:1-100
begin
loop
dbms_output.put_line(v_i);
exit when v_i >=100;
v_i := v_i + 1;
end loop;
end;
使用while实现:
declare
v_i number(5) :=1;
begin
while v_i <= 100 loop
dbms_output.put_line(v_i);
v_i := v_i + 1;
end loop;
end;
使用for...in...loop...end loop;实现:
begin
for c in 1..100 loop
dbms_output.put_line(c);
end loop;
end;
输出2-100之间的质数
sql;">
declare
v_i number(3):= 2;
v_j number(3):= 2;
v_flag number(1):= 1;
begin
while v_i<=100 loop
while v_j<=sqrt(v_i) 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;
sql;">
declare
v_flag number(1):= 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;
可以用goto改进一下:
sql;">
declare
v_flag number(1):= 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;
goto label;
end if;
end loop;
<
salary from employees where department_id = 80;
begin
emp_sal_cursor; 提取游标
emp_sal_cursor into v_sal;
while emp_sal_cursor%found loop
dbms_output.put_line('salary:'||v_sal);
fetch emp_sal_cursor into v_sal;
end loop; 关闭游标
emp_sal_cursor;
end;
可以进行优化如下:
sql;">
declare
v_empid employees.employee_id%type;
v_lastName employees.last_name%type;
v_sal employees.salary%type;
cursor emp_sal_cursor is select employee_id,last_name,salary from employees where
department_id = 80;
begin
open emp_sal_cursor;
fetch emp_sal_cursor into v_empid,v_lastName,v_sal;
while emp_sal_cursor%found loop
dbms_output.put_line('employee_id:'||v_empid||','||'last_name:'||v_lastName||','||'salary:'||v_sal);
fetch emp_sal_cursor into v_empid,v_sal;
end loop;
close emp_sal_cursor;
end;
或者使用记录再优化一下:
sql;">
declare
type emp_record is record(
v_empid employees.employee_id%type,v_lastName employees.last_name%type,v_sal employees.salary%type
);
v_emp_record emp_record;
cursor emp_sal_cursor is select employee_id,salary from employees where
department_id = 80;
begin
open emp_sal_cursor;
fetch emp_sal_cursor into v_emp_record;
while emp_sal_cursor%found loop
dbms_output.put_line('employee_id:'||v_emp_record.v_empid||','||'last_name:'||
v_emp_record.v_lastName||','||'salary:'||v_emp_record.v_sal);
fetch emp_sal_cursor into v_emp_record;
end loop;
close emp_sal_cursor;
end;
sql;">
declare
cursor emp_sal_cursor is select employee_id,salary from employees where
department_id = 80;
begin
for c in emp_sal_cursor loop
dbms_output.put_line('employee_id:'||c.employee_id||','||'last_name:'||c.last_name||','||'salary:'||c.salary);
end loop;
end;
sql;">
declare
cursor emp_cursor is select employee_id,salary from employees;
v_empid employees.employee_id%type;
v_sal employees.salary%type;
v_temp number(4,2);
begin
open emp_cursor;
fetch emp_cursor into v_empid,v_sal;
while emp_cursor%found loop
if v_sal < 5000 then v_temp:=0.05;
elsif v_sal < 10000 then v_temp:=0.03;
elsif v_sal < 15000 then v_temp:=0.02;
else v_temp:=0.01;
end if;
dbms_output.put_line(v_empid||','||v_sal);
update employees
set salary = salary * (1+v_temp)
where employee_id = v_empid;
fetch emp_cursor into v_empid,v_sal;
end loop;
close emp_cursor;
end;
用for循环实现
sql;">
declare
cursor emp_cursor is select employee_id,salary from employees;
v_temp number(4,2);
begin
for c in emp_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;
更新员工salary(涨工资10),如果该员工没有找到,则打印“查无此人”信息:
sql;">
begin
update employees
set salary = salary + 10
where employee_id = 1001;
if sql%notfound then dbms_output.put_line('查无此人');
end if;
end;
:(预定义异常,)
100;
dbms_output.put_line(v_sal);
exception
when too_many_rows then dbms_output.put_line('输出的行数过多');
when others then dbms_output.put_line('出现其它的异常了');
end;
sql;">
declare
e_deleteid_exception exception;
pragma exception_init(e_deleteid_exception,-2292);
begin
delete from employees
where employee_id = 100;
exception
when e_deleteid_exception then dbms_output.put_line('违反了完整性约束,故不能删除此用户');
when others then dbms_output.put_line('出现其它的异常了');
end;
10000 then raise e_sal_hight;
end if;
exception
when e_sal_hight then dbms_output.put_line('工资太高了');
when others then dbms_output.put_line('出现其它的异常了');
end;
sql;">
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id = 1001;
exception
when no_data_found then dbms_output.put_line('未找到此数据');
when others then dbms_output.put_line('出现其它的异常了');
end;
更新指定员工工资,如工资小于300,则加100,对NO_DATA_FOUND异常,TOO_MANY_ROWS进行处理。
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id = 1001;
if v_sal < 300 then update employees set salary = salary + 100 where employee_id =101;
end if;
exception
when no_data_found then dbms_output.put_line('未找到此数据');
when too_many_rows then dbms_output.put_line('输出的行数太多了');
when others then dbms_output.put_line('出现其它的异常了');
end;
sql;">
declare
no_result exception;
begin
update employees set salary = salary + 100 where employee_id = 1001;
if sql%notfound then raise no_result;
end if;
exception
when no_result then dbms_output.put_line('查无此数据,更新失败');
when others then dbms_output.put_line('出现其它异常');
end;
sql;">
create or replace function hello_world
return varchar2
is (相当于declare,可以在其后面定义变量、记录、游标)
begin
return 'helloworld';
end;
存储函数的调用:
begin
dbms_output.put_line(hello_world);
end;
或者:
select hello_world from dual;
sql;">
create or replace function hello_world1(v_logo varchar2)
return varchar2
is
begin
return 'helloworld'||v_logo;
end;
调用:
select hello_world1('shellway') from dual
或者:
begin
dbms_output.put_line(hello_world1('shellway'));
end;
sql;">
create or replace function get_sysdate
return varchar2
is
begin
return to_char(sysdate,'yyyy-MM-dd HH24:mi:ss');
end;
函数,两个数相加
sql;">
create or replace function add_param(v_num1 number,v_num2 number)
return number
is
v_num3 number(10);
begin
v_num3 := v_num1 + v_num2;
return v_num3;
end;
调用:
select add_param(2,5) from dual;
或者:
begin
dbms_output.put_line(add_param(5,4));
end;
sql;">
create or replace function get_sal(dept_id number)
return number
is
v_sumsal number(10) := 0;
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
end loop;
return v_sumsal;
end;
调用:
select get_sal(80) from dual;
sql;">
create or replace function get_sal(dept_id number,total_count out number)
return number
is
v_sumsal number(10) := 0;
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
total_count := 0;
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
total_count := total_count + 1;
end loop;
return v_sumsal;
end;
调用:
declare
v_count number(4);
begin
dbms_output.put_line(get_sal(80,v_count));
dbms_output.put_line(v_count);
end;
sql;">
create or replace procedure get_sal1(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
get_sal1(80,v_sal);
end;
sql;">
create or replace procedure add_sal(dept_id number,temp out number)
is
cursor sal_cursor is select employee_id,salary,hire_date
from employees where department_id = dept_id;
v_temp number(4,2):=0;
begin
temp := 0;
for c in sal_cursor loop
if to_char(c.hire_date,'yyyy') < '1995' then v_temp:=0.05;
elsif to_char(c.hire_date,'yyyy') < '1998' then v_temp:=0.03;
else v_temp:=0.01;
end if;
update employees
set salary = salary * (1+v_temp)
where employee_id = c.employee_id;
temp := temp + c.salary*v_temp;
end loop;
dbms_output.put_line(temp);
end; 调用:
declare
v_i number(10):=0;
begin
add_sal(80,v_i);
end;
sql;">
create or replace trigger update_emp_trigger
after
update on employees
for each row (行级触发器,即每更新一条记录就会输出一次'helloworld',若没有这语句则是语句级触发器)
begin
dbms_output.put_line('helloworld');
end;