mysql儲存過程簡單例項

dawn009發表於2015-05-29
 MySql儲存過程簡單例項:                                                    
                                                   /********************* 建立表 *****************************/
                                                    delimiter //
                                                    
                                                    DROP TABLE if exists test //
                                                    
                                                    CREATE TABLE test(
                                                      id int(11) NULL
                                                    ) //
                                                    
                                                    /********************** 最簡單的一個儲存過程 **********************/
                                                    drop procedure if exists sp//
                                                     CREATE PROCEDURE sp() select 1 //
                                                     
                                                     call sp()//
                                                     
                                                    /********************* 帶輸入引數的儲存過程  *******************/
                                                    
                                                    drop procedure if exists sp1 //
                                                    
                                                    create procedure sp1(in p int)
                                                    comment 'insert into a int value'
                                                    begin
                                                      /* 定義一個整形變數 */
                                                      declare v1 int;
                                                      
                                                      /* 將輸入引數的值賦給變數 */
                                                      set v1 = p;
                                                      
                                                      /* 執行插入操作 */
                                                      insert into test(id) values(v1);
                                                    end
                                                    //
                                                    
                                                    /* 呼叫這個儲存過程  */
                                                    call sp1(1)//
                                                    
                                                    /* 去資料庫檢視呼叫之後的結果 */
                                                    select * from test//
                                                    
                                                     /****************** 帶輸出引數的儲存過程 ************************/
                                                    
                                                    drop procedure if exists sp2 //
                                                    create procedure sp2(out p int)
                                                    /*這裡的DETERMINISTIC子句表示輸入和輸出的值都是確定的,不會再改變.我一同事說目前mysql並沒有實現該功能,因此加不加都是NOT DETERMINISTIC的*/
                                                    DETERMINISTIC
                                                    begin
                                                      select max(id) into p from test;
                                                    end
                                                    //
                                                    
                                                    /* 呼叫該儲存過程,注意:輸出引數必須是一個帶@符號的變數 */
                                                    call sp2(@pv)//
                                                    
                                                    /* 查詢剛剛在儲存過程中使用到的變數 */
                                                    select @pv//                                                    
                                                    
                                                    /******************** 帶輸入和輸出引數的儲存過程 ***********************/
                                                    
                                                    drop procedure if exists sp3 //
                                                    create procedure sp3(in p1 int , out p2 int)
                                                    begin
                                                    
                                                      if p1 = 1 then
                                                        /* 用@符號加變數名的方式定義一個變數,與declare類似 */
                                                        set @v = 10;
                                                      else
                                                        set @v = 20;
                                                      end if;
                                                      
                                                      /* 語句體內可以執行多條sql,但必須以分號分隔 */
                                                      insert into test(id) values(@v);
                                                      select max(id) into p2 from test;
                                                      
                                                    end
                                                    //
                                                    
                                                    /* 呼叫該儲存過程,注意:輸入引數是一個值,而輸出引數則必須是一個帶@符號的變數 */
                                                    call sp3(1,@ret)//
                                                    
                                                    select @ret//
                                                    
                                                    /***************** 既做輸入又做輸出引數的儲存過程 ***************************************/
                                                    
                                                    drop procedure if exists sp4 //
                                                    create procedure sp4(inout p4 int)
                                                    begin
                                                       if p4 = 4 then
                                                          set @pg = 400;
                                                       else
                                                          set @pg = 500;
                                                       end if; 
                                                       
                                                       select @pg;
                                                       
                                                    end//
                                                    
                                                    call sp4(@pp)//
                                                    
                                                    /* 這裡需要先設定一個已賦值的變數,然後再作為引數傳入 */
                                                    set @pp = 4//
                                                    call sp4(@pp)//
                                                    
出處:http://www.cnblogs.com/zhuawang/p/4185302.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29119536/viewspace-1676417/,如需轉載,請註明出處,否則將追究法律責任。

相關文章