/*声明部分,以declare开头*/
declare v_id integer;
v_name varchar(20);
cursor c_emp is select * from employee where emp_id=3;
/*执行部分,以begin开头*/
begin open c_emp; //打开游标
loop
fetch c_emp into v_id,v_name; //从游标取数据
exit when c_emp%notfound ;
end loop ;
close c_emp; //关闭游标
dbms_output.PUT_LINE(v_name);
/*异常处理部分,以exception开始*/
exception
when no_data_found then
dbms_output.PUT_LINE('没有数据');
end ;
2. 控制结构
PL/SQL程序段中有三种程序结构:条件结构、循环结构和顺序结构。
1) 条件结构
与其它语言完全类似,语法结构如下:
if condition then
statement1
else
statement2
end if ;
2) 循环结构
这一结构与其他语言不太一样,在PL/SQL程序中有三种循环结构:
a. loop … end loop;
b. while condition loop … end loop;
c. for variable in low_bound . . upper_bound loop … end loop;
Create or replace procedure test_procedure as
V_f11 number :=1; /*声明变量并赋初值*/
V_f12 number :=2;
V_f21 varchar2(20) :='first';
V_f22 varchar2(20) :='second';
Begin
Insert into t1 values (V_f11, V_f21);
Insert into t1 values (V_f12, V_f22);
End test_procedure; /*test_procedure可以省略*/