mysql預存程序的學習(一)

來源:互聯網
上載者:User

標籤:判斷   入參   預存程序   mysql   in out   ota   from   create   符號   

建立一個預存程序

create procedure myprocess()

begin

end;

為了避免預存程序中分號(";")結束語句,我們使用分隔字元來判斷該段命令是否已經結束了。

所以我們可以以$符號來作為結束語(亦可以用其他)

delimiter $

create procedure myprocess()

begin

end $

其實就和java裡面的新增加一個方法一樣 只不過這裡是mysql的文法

方法裡面也可以傳參數

這裡傳參數的規則有 in out inout 

先說in 

in為入參,如果為入參,此參數只能用來傳,最後的值是不會改變的,就是最後調用預存程序的時候如果要獲得in入參的值,不管中間做了什麼操作,這個值只能是最開始傳的那個值

delimiter $

create procedure myprocess( in userid int)

begin

select * from u_user where id = userid;
end $

調用過程

call myprocess(2);

out為出參

delimiter $

create procedure myprocess( out total int)

begin

select count(1) into total  from u_user ;
end $

調用過程

注意 出參的話必須要用@打頭 不然輸出不了在預存程序裡面直接給total賦值

調用過程

CALL myprocess(@total);
select @total;

inout 既是入參也是出參,就是將傳過來的參數先進行一系列的運算之後把結果再次賦值給這個參數 輸出

delimiter $

create procedure myprocess( INOUT total int)

begin
select count(1) into total from u_user where id = total;

end $

調用過程因為既是入參也是出參,所以先賦值給他,最後再查詢這個值

set @total =2;
CALL myprocess(@total);

select @total;

先寫到這裡。。

 

mysql預存程序的學習(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.