- PL/SQL中的运算符大部分和Java中的一样。
- PL/SQL最大的作用就是处理数据:
- PL/SQL不能直接将数据查询出来:出现Select语句中缺少into子句。
- 每次查询只能查询一条数据赋值给变量:出现实际返回的行数超出请求的行数,需要使用游标。出现错误如下:表要先建好
declare
v_stu_id number;
v_stu_name varchar2(10);
v_stu_age number;
begin
select stu_id,stu_name,stu_age
into v_stu_id,v_stu_name,v_stu_age/*这里查询出来要赋值给变量*/
from tab_stu;
end;
下面就查询一条记录,并输出
declare
v_stu_id number;
v_stu_name varchar2(10);
v_stu_age number;
begin
select stu_id,stu_name,stu_age
into v_stu_id,v_stu_name,v_stu_age
from tab_stu
where stu_id=1;
dbms_output.put_line('stu_id:'||v_stu_id);
dbms_output.put_line('stu_name:'||v_stu_name);
dbms_output.put_line('stu_age:'||v_stu_age);
end;
stu_id:1
stu_name:张三
stu_age:21
插入数据,其他的DML语句也就类似了。
%type和%rowtype
- 当不知道要输出表的字段的类型的时候,可以使用%type。来定义变量。如下:
--%type的使用
declare
v_stu_id tab_stu.stu_id%type;
v_stu_name tab_stu.stu_name%type;
v_stu_age tab_stu.stu_age%type;
begin
select stu_id,stu_name,stu_age
into v_stu_id,v_stu_name,v_stu_age
from tab_stu
where stu_id=1;
dbms_output.put_line('stu_id:'||v_stu_id);
dbms_output.put_line('stu_name:'||v_stu_name);
dbms_output.put_line('stu_age:'||v_stu_age);
end;
%rowtype的使用
--%rowtype的使用
declare
v_tab_stu_row tab_stu%rowtype;
begin
select stu_id,stu_name,stu_age
into v_tab_stu_row.stu_id,v_tab_stu_row.stu_name,v_tab_stu_row.stu_age
from tab_stu
where stu_id=2;
dbms_output.put_line('stu_id:'||v_tab_stu_row.stu_id);
dbms_output.put_line('stu_name:'||v_tab_stu_row.stu_name);
dbms_output.put_line('stu_age:'||v_tab_stu_row.stu_age);
end;
第二种用法
--%rowtype的使用
declare
v_tab_stu_row tab_stu%rowtype;
begin
select *
into v_tab_stu_row
from tab_stu
where stu_id=3;
dbms_output.put_line('stu_id:'||v_tab_stu_row.stu_id);
dbms_output.put_line('stu_name:'||v_tab_stu_row.stu_name);
dbms_output.put_line('stu_age:'||v_tab_stu_row.stu_age);
end;
