手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle数据中的PL/SQL介绍

[日期:2013-10-02] 来源:Linux社区  作者:facekbook [字体:大 中 小]
  • 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;

linux
相关资讯       PL/SQL 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款