Oracle資料中的PL/SQL介紹

來源:互聯網
上載者:User

什麼是Pl/SQL:

  • --PL/SQL的寫法declare  /*申明部分:在此申明PL/SQL用到的變數,類型及遊標,以及局部預存程序和函數*/  str varchar2(20);  i number;begin  /*執行部分:過程及SQL語句,即程式的主要部分*/  str :='Hello Oracle';  i:='aa';  dbms_output.put_line(str);exception /*執行異常部分:錯誤處理*/ when others then dbms_output.put_line('錯誤');end;

  • declare str varchar2(10):='Hello ';/*定義時賦值*/ str2 varchar2(10); i constant number:=100;/*定義常量,但是必須在申明時賦值*/begin    str2:= 'Oracle';/*在begin中賦值*/  dbms_output.put_line(str||str2);/*\\是Oracle的串連符*/  dbms_output.put_line(i);exception   when others then    dbms_output.put_line('出錯了');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; 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:1stu_name:張三stu_age:21

    •  --插入 declare  begin      insert into tab_stu(stu_id,stu_name,stu_age) values(4,'趙六','22');        commit;/*記得提交*/ end;

      • --%type的使用declarev_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的使用declarev_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;

  • --可變數組的使用varraydeclare type strings is varray(6) of varchar(10);--這裡其實就像定義了一個對象 var_array strings:=strings('張三','孫八','孫七','趙六','王五','李四');--這裡就是這個對象的具體實現,和定義begin  dbms_output.put_line(var_array(1));  dbms_output.put_line(var_array(2));  dbms_output.put_line(var_array(3));  dbms_output.put_line(var_array(4));  dbms_output.put_line(var_array(5));  dbms_output.put_line(var_array(6));end;
    輸出
    張三孫八孫七趙六王五李四

  • --可變資料類型tabledeclare   type tableStrings is table of varchar2(10)  index by binary_integer;--定義table類型的下標是二進位類型的無限增長的  var_table tableStrings;--為定義好的類型,定義一個變數begin  var_table(1):='Hello';--為變數的索引賦值  var_table(999):='Word';--為變數的索引賦值  dbms_output.put_line(var_table(1)||' '||var_table(999));end;
    輸出:Hello Word
  • --可變資料類型table的使用2declare type table_row is table of tab_stu%rowtype index by binary_integer; var_tab_stu table_row;begin  select stu_id,stu_name   into var_tab_stu(100).stu_id,var_tab_stu(100).stu_name  from tab_stu  where stu_id=2;  dbms_output.put_line(var_tab_stu(100).stu_id ||' '|| var_tab_stu(100).stu_name);end;
    輸出:2 李四
  • --record理解為Java中的集合declare type v_record is record(      re_id number,--可以是普通變數      re_name tab_stu.stu_name%type,--可以是表中的一個欄位的變數      r_tab_stu tab_stu%rowtype--也可以是表中的一行 ); v_r v_record;--需要賦給變數在使用時begin  select stu_id,stu_name,stu_age  into v_r.re_id,  v_r.re_name,  v_r.r_tab_stu.stu_age  from tab_stu  where stu_id=1;  dbms_output.put_line(v_r.re_id);  dbms_output.put_line(v_r.re_name);  dbms_output.put_line(v_r.r_tab_stu.stu_age);end;



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.