MySQL 存儲過程參數用法 in, out, inout(轉)

          ?

          MySQL 存儲過程參數有三種類型:in、out、inout。它們各有什么作用和特點呢?

          一、MySQL 存儲過程參數(in)

          MySQL 存儲過程 “in” 參數:跟 C 語言的函數參數的值傳遞類似, MySQL 存儲過程內部可能會修改此參數,但對 in 類型參數的修改,對調用者(caller)來說是不可見的(not visible)。

          										
          drop procedure if exists pr_param_in;
          
          create procedure pr_param_in
          (
             in id int -- in 類型的 MySQL 存儲過程參數
          )
          begin
             if (id is not null) then
                set id = id + 1;
             end if;
          
             select id as id_inner;
          end;
          
          										
          set @id = 10;
          
          call pr_param_in(@id);
          
          select @id as id_out;
          
          										
          mysql> call pr_param_in(@id);
          +----------+
          | id_inner |
          +----------+
          |       11 |
          +----------+
          
          mysql> select @id as id_out;
          +--------+
          | id_out |
          +--------+
          | 10     |
          +--------+
          

          可以看到:用戶變量 @id 傳入值為 10,執行存儲過程后,在過程內部值為:11(id_inner),但外部變量值依舊為:10(id_out)。

          二、MySQL 存儲過程參數(out)

          MySQL 存儲過程 “out” 參數:從存儲過程內部傳值給調用者。在存儲過程內部,該參數初始值為 null,無論調用者是否給存儲過程參數設置值。

          										
          drop procedure if exists pr_param_out;
          
          create procedure pr_param_out
          (
             out id int
          )
          begin
             select id as id_inner_1;  -- id 初始值為 null
          
             if (id is not null) then
                set id = id + 1;
          
                select id as id_inner_2;
             else
                select 1 into id;
             end if;
          
             select id as id_inner_3;
          end;
          
          										
          set @id = 10;
          
          call pr_param_out(@id);
          
          select @id as id_out;
          
          										
          mysql> set @id = 10;
          mysql>
          mysql> call pr_param_out(@id);
          +------------+
          | id_inner_1 |
          +------------+
          |       NULL |
          +------------+
          
          +------------+
          | id_inner_3 |
          +------------+
          |          1 |
          +------------+
          
          mysql> select @id as id_out;
          +--------+
          | id_out |
          +--------+
          | 1      |
          +--------+
          

          可以看出,雖然我們設置了用戶定義變量 @id 為 10,傳遞 @id 給存儲過程后,在存儲過程內部,id 的初始值總是 null(id_inner_1)。最后 id 值(id_out = 1)傳回給調用者。

          三、MySQL 存儲過程參數(inout)

          MySQL 存儲過程 inout 參數跟 out 類似,都可以從存儲過程內部傳值給調用者。不同的是:調用者還可以通過 inout 參數傳遞值給存儲過程。

          										
          drop procedure if exists pr_param_inout;
          
          create procedure pr_param_inout
          (
             inout id int
          )
          begin
             select id as id_inner_1;  -- id 值為調用者傳進來的值
          
             if (id is not null) then
                set id = id + 1;
          
                select id as id_inner_2;
             else
                select 1 into id;
             end if;
          
             select id as id_inner_3;
          end;
          
          										
          set @id = 10;
          
          call pr_param_inout(@id);
          
          select @id as id_out;
          
          										
          mysql> set @id = 10;
          
          mysql>
          mysql> call pr_param_inout(@id);
          +------------+
          | id_inner_1 |
          +------------+
          |         10 |
          +------------+
          
          +------------+
          | id_inner_2 |
          +------------+
          |         11 |
          +------------+
          
          +------------+
          | id_inner_3 |
          +------------+
          |         11 |
          +------------+
          mysql>
          mysql> select @id as id_out;
          +--------+
          | id_out |
          +--------+
          | 11     |
          +--------+
          

          從結果可以看出:我們把 @id(10),傳給存儲過程后,存儲過程最后又把計算結果值 11(id_inner_3)傳回給調用者。 MySQL 存儲過程 inout 參數的行為跟 C 語言函數中的引用傳值類似。

          通過以上例子:如果僅僅想把數據傳給 MySQL 存儲過程,那就使用“in” 類型參數;如果僅僅從 MySQL 存儲過程返回值,那就使用“out” 類型參數;如果需要把數據傳給 MySQL 存儲過程,還要經過一些計算后再傳回給我們,此時,要使用“inout” 類型參數。

          ?

          posted on 2008-10-09 12:50 nonels 閱讀(20592) 評論(7)  編輯  收藏 所屬分類: 學習筆記

          評論

          # re: MySQL 存儲過程參數用法 in, out, inout(轉)[未登錄] 2009-04-22 10:49 jezz

          還不錯!  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉) 2009-11-13 17:16 guolili

          enen,bu cuo!!  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉) 2010-08-01 02:09 ding

          寫得好  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉)[未登錄] 2011-10-19 01:23 Dan

          好文  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉) 2013-09-08 10:05 Ice_Xue

          謝謝樓主,講得很詳細!!!學習了  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉) 2013-11-14 18:09 舒偉

          寫的很清楚,條理清晰,易懂。對我幫助很大,很感謝。  回復  更多評論   

          # re: MySQL 存儲過程參數用法 in, out, inout(轉) 2014-09-02 00:04 zhangsw

          博主將的很到位,邏輯很清楚,謝謝哦。  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類(16)

          隨筆檔案(16)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 洞口县| 莒南县| 景洪市| 阳谷县| 台前县| 乌兰浩特市| 神木县| 永春县| 六安市| 新野县| 贵州省| 辽阳县| 承德市| 镇安县| 大悟县| 嘉义县| 大连市| 宁南县| 武冈市| 神池县| 汉寿县| 贵州省| 西乌| 阳春市| 长寿区| 大悟县| 民丰县| 贡嘎县| 沙洋县| 常州市| 竹山县| 桓台县| 靖边县| 鄂伦春自治旗| 彭州市| 施秉县| 科技| 长阳| 临潭县| 平顺县| 许昌县|