mysql存儲過程的學習(一)
創建一個存儲過程
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存儲過程的學習(一)