mysql預存程序文法

來源:互聯網
上載者:User

mysql教程預存程序文法

變數定義:

declare variable_name [,variable_name...] datatype [default value];


其中,datatype為mysql的資料類型,如:int, float, date, varchar(length)

例:

declare l_int int unsigned default 4000000; declare l_numeric numeric(8,2) default 9.95; declare l_date date default '1999-12-31'; declare l_datetime datetime default '1999-12-31 23:59:59'; declare l_varchar varchar(255) default 'this will not be padded';

變數賦值

set 變數名 = 運算式值 [,variable_name = expression ...]
 

參數

mysql預存程序的參數用在預存程序的定義,共有三種參數類型,in,out,inout

create procedure|function([[in |out |inout ] 參數名 資料類形...])

 

in 輸入參數

表示該參數的值必須在調用預存程序時指定,在預存程序中修改該參數的值不能被返回,為預設值

out 輸出參數

該值可在預存程序內部被改變,並可返回

inout 輸入輸出參數

調用時指定,並且可被改變和返回

 


建預存程序:

 

文法:

create procedure p()

begin

/*此預存程序的本文*/

end 
create procedure productpricing()

begin

    select avg(pro_price) as priceaverage

    from products;

end;

# begin…end之間是預存程序的主體定義

# mysql的分界符是分號(;) 

                        

調用預存程序的方法是:

 

# call加上過程名以及一個括弧

# 例如調用上面定義的預存程序

call productpricing();

聯繫我們

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