ORACLE預存程序(一)之初次見面

來源:互聯網
上載者:User

————————————————      Hello World           ——————————————————————
create or replace procedure HelloWorld ASbegin  insert into MYPROCEDURE(id,name,city,MANAGER)values(1,'ss','ss','ss');end HelloWorld;

預存程序一般分分三部分:聲明部分、執行部分、異常部分

變數聲明:

文法 變數名+變數類型

create or replace procedure HelloWorld ASvariable_a varchar(8);variable_b varchar(5);begin  insert into MYPROCEDURE(id,name,city,MANAGER)values(1,'ss','ss','ss');end HelloWorld;

if判斷:

文法:if (運算式) then begin end;end if;

create or replace procedure test(prameter in number) is     begin        if x > 0 then           begin            x := 0 - x;          end;       end if;          if x = 0 then           begin             x := 1;          end;       end if;end test;

for迴圈遍曆:
文法:for ... in ... loop
(1)迴圈遍曆遊標

create or replace procedure test() as  Cursor cursorName is    select table from student;  name varchar(20);begin  for name in cursorName LOOP      begin          dbms_output.putline(name);        end;    end LOOP;end test;

(2)迴圈遍曆數組

create or replace procedure test(array in TestArray) as   i number;begin  i:=1;  for i in TestArray LOOP    dbms_output.put_line(array(i));end test;

while迴圈遍曆:

文法:while(運算式)loop

create or replace procedure test(i in number) asbegin  while i < 10 LOOP    begin      i := i + 1;        end;    end LOOP;  dbms_output.put_line(i);end test;

大概的知道預存程序是怎麼個情況,接下來通過執行個體逐步瞭解預存程序

建表:

create table USER_INFO(  ID      VARCHAR2(4),  NAME    VARCHAR2(15),  PWD     VARCHAR2(15),  ADDRESS VARCHAR2(30))

預存程序:

create or replace procedure AddNewUser(n_id      user_info.id%TYPE,                                       n_name    user_info.name%TYPE,                                       n_pwd     user_info.pwd%TYPE,                                       n_address user_info.address%TYPE) isbegin  INSERT INTO user_info    (id, name, pwd, address)  VALUES    (n_id, n_name, n_pwd, n_address);end AddNew

預存程序調用:

預存程序的調用:(1)PL/SQL匿名塊調用DECLARE n_id user_info.id%TYPE := 'u002'; n_name user_info.name%TYPE := 'wish'; n_pwd user_info.pwd%TYPE := 'history'; n_add user_info.address%TYPE := 'shanghai'; BEGIN AddNewUser(n_id,n_name,n_pwd,n_add); DBMS_OUTPUT.PUT_LINE('使用者 ' || n_name || ' 已經成功插入'); END;

(在PLSQL Developer中執行匿名塊時,F8執行後,按F10儲存才可更新到資料庫表中,上述案例測試成功。)

(2)JDBC中調用預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.