PL/SQL語法

PL/SQL語法sql

 

因爲pl/sql是編譯後執行的,而sql語句是未經編譯的,所以pl/sql語句在執行速度上更快,同時也減小了客戶機和服務器的傳輸。數據庫

 

基本結構服務器

 

DECLAREoop

   聲明變量、常量、用戶定義的數據類型以及遊標等fetch

   可選spa

 

BEGIN指針

   主程序體內存

 

EXCEPTIONci

    異常處理程序,當程序出現錯誤時,執行這一部分。字符串

 

END;

 

 

聲明部分、執行部分、異常處理部分。執行部分是必須的,其餘倆部分可選。

 

 

 

Pl/sql符號

 

+     

-     

*     

/     

=    等於

:=賦值 

>    大於

<    小於

(

)

;     語句結束

%    屬性指示符

   項目分隔符

@    數據庫鏈接指示符

/     字符串分界符

   綁定變量指示符

**    指數操做符

<>    不等於

=     不等於

~=      不等於

^=      不等於

<=       小於等於

>=       大於等於

=    賦值

=>     連接操做符

..      範圍操做符

||      串鏈接

--      單行註釋

/* */    多行註釋

 

 

常量和變量

 

一、定義常量

       <常量名> constant <數據類型> = <>

 

      pass_Score constant INTEGER = 60

 

 

   2、定義變量

      <變量><數據類型>

 

      address VARCHAR220;-------------------初始值爲NULL

 

 

數據類型

   

    1 數字類型: number  pls_integer、  binary integer

 

    2 字符類型: varchar2char longnchar  nvarchar2

                varchar2 和數據庫中的varchar2不一樣,最大長度爲32767

   

 

    3日期類型:date

 

    4 布爾類型   boolean---------------true   false  null

 

 

5自定義類型

  type  <類型名> is <數據類型>

 

Oracle中 容許定義兩種數據類型   record(記錄類型)和table(表類型)

 

    type   user_record is record(

 

         id number(5) ,

         name varchar2(20),

         sex char(1)

   )  

 

   使用:

 

     auser  user_record

     auser.id     auser.name  ..........

 

 

  另外,pl/sql還提供了兩種特殊的變量   %TYPE%ROWTYPE,用來聲明與表的列相匹配的變量和用戶定義數據類型。前一個表示單屬性的數據類型,後一個表示整個屬性列表的結構

 

 

上面例子能夠改寫爲:

 

   type   user_record is record(

 

         Id   USERS.id%TYPE

              Users是一個表

            Id的和 users表中的id類型相同

 

         Name USERS.NAME%TYPE

         Sex USERS.SEX%TYPE

     

   )  

 


user_record  USERS%ROWTYPE;

 

user_record.

 

aaa EMPLOYEES%ROWTYPE

 

type aaa is record(

    Employee_id number

    First_name varchar2()

............  

 

)

 

 

結構控制語句

 

         選擇結構

        

         If ...then...     If(){}

         If...then...else...

         If...then...elseif

 

         End if

         case

 

 

       

 if    no  = 100    then

  

insert into users()

 

end if;

 

 

 if    no  = 100    then

  

Insert into users();

 

else

 

Insert into.....

 

end if;

 

 

If score>90 then

 

  Score := score-5;

else if score<60 then

Score:=score+5;

end if;

 

 

case------相似於switch

 

num=  case  name

       when 'A' then '0001'

       when 'B' then '0003'

       ....

 

       else 'no user'

 

       end;

 

null結構

Declare

Num1 number;

Num2 number;

Res varchar210

 

Begin

If num1<num2 then

  Res:='yes'

Else

   Res:='no';

End if;

 

End;

 

 

Declare

Num1 number;

Num2 number;

Res varchar210

 

Begin

 

If num1>num2 then

  Res:='no'

 

Else

   Res:='yes';

End if;

 

End;

 

 

num1=1 num2=null時,結果將是不一樣的

 

因此要在程序中加入null值檢查

 

If    num1 is  null   or   num2 is null  then  res :=noresult

 

Else if。。。。。。。。。。。

 

 

 

 

循環結構

 

 

1  loop...exit...end

 

 Control_var := 0;   循環因子

 

 loop--------------------------------------------循環開始

   If control_var>5 then

      Exit;

   End if;

Control_var:=Control_var+1;

.......

End loop;------------------------------------------循環結束

 

 

 

2  loop...exit when...end

 

 Control_var := 0;

Loop

Exit when  Control_var>5

Control_var:=Control_var+1;

 

End loop;

 

 

3.while ...loop...end

 

    Control_var := 0;

   While Control_var<5 loop

Control_var:=Control_var+1;

......

End loop;

 

 

4.for... In...loop...end

   

for control_var in 0..5 loop

 

.......

 

End loop;

declare

 

control_var number;

 

begin

  control_var:=0;

  

  for control_var in 0..5 loop

    

  dbms_output.put_line('123');

  

  end loop;

 

end;

 

 

遊標   cursor

 

相似於C語言中的指針,但指針的數據是定義好的,遊標用select語句從表或視圖中選出須要的數據,而後放入內存中,遊標指向查詢結果的首部。使用遊標對此查詢結果進行一些取值操做,隨着遊標的移動,也就訪問到了全部的行。

 

遊標分爲兩種:

   隱示遊標-----------不須要用戶定義。

 

   顯示遊標-------  用戶本身定義

  

 

顯示遊標處理包括4個步驟:

1 聲明遊標

2 爲查詢打開遊標

3 將結果提取到pl/sql變量中

4 關閉遊標

 

例子:

 

declare

 

 user_id users.id%type;

 user_name users.name%type;

 user_age users.age%type;

 

 cursor user_cur is select * from users; -- 定義遊標

 

begin

  

   open user_cur; -- 打開遊標

   

   fetch user_cur into user_id,user_name,user_age; --將第一行數據存入變量,遊標後移

   

   loop --Exit when user_cur%NOTFOUND

        Exit when not user_cur%FOUND;---若是遊標到結尾結束

        

        if user_age>20 then

          dbms_output.put_line('老人');

         else

           dbms_output.put_line('年輕人');    

         end if;  

 

         fetch user_cur into user_id,user_name,user_age;

         

     end loop;

     

     close user_cur;    

end;

 

 

隱式遊標操做

 

Select id ,name,sex into user_id,user_name,user_sex from users where id =100;

 

 

 

遊標的屬性操做

 

%FOUND-----是否找到遊標

 

%NOTFOUND---是否沒找到遊標

 

%ROWCOUNT------遊標行數

 

%ISOPEN

 

----------------------------------------------------------------------------------------------------------------

動態遊標

 

 

聲明:

 

TYPE<類型名>  IS  REF  CURSOR  RETURN <返回類型>

 

 

Type user_cur_type is ref cursor return users%rowtype;

 

User_cur   user_cur_type;

 

也可不加return-------即,非受限遊標變量

 

Type user_cur_type is ref cursor;

User_cur   user_cur_type;

 

 

 

 

 

 

存儲過程:

 

Create or replace mycountin_sex in user.ssex%type

 

As

 

Out_num number;

 

Begin

 

If in_sex ='m' then

Select count(sex) into out_num from users where sex='m';

 

Dbms_output.put_line(out_num);

Else

Select count(sex) into out_num from users where sex='f;

 

Dbms_output.put_line(out_num);

 

End if;

 

End count;

 

 

執行

 

Execute count(‘m’);

 

 

參數類型:

 

In類型參數

 

Out類型參數

 

In out 類型參數

 

Create or replace produce doublein_num in number,out_num out numberas

Begin

    Out_num:=in_num*2;

End double;

相關文章
相關標籤/搜索