標籤:判斷 入參 預存程序 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預存程序的學習(一)