mysql預存程序例子

來源:互聯網
上載者:User

標籤:arch   out   varchar   定義變數   limit   inner   mysql預存程序   procedure   建立   

/*
定義變數
方式1:set @變數名=值;
方式2:select 值 into @變數名;
方式3:declare 變數名 類型(字串類型加範圍) default 值;

in參數 入參的值會僅在預存程序中起作用
out參數 入參的值會被置為空白,儲存中計算的值會影響外面引用該變數的值
inout參數 入參的值不會被置為空白,儲存中計算的值會影響外面引用該變數的值
*/
use mysql;
/*建立1個預存程序*/
delimiter $$
DROP PROCEDURE IF EXISTS porc_person_02;
CREATE PROCEDURE porc_person_02(IN p1 INT, OUT p2 INT, INOUT p3 VARCHAR(20))
BEGIN
DECLARE innerp1 VARCHAR(10) DEFAULT ‘this is innerp1‘;
DECLARE innerp2 VARCHAR(10) DEFAULT ‘this is innerp2‘;
SET p1=10;
SET p2=20;
SET p3=‘this is 字串‘;


if p1=10 then
select ‘p1 is 10‘;
end if;

if p1=p2 then
select ‘p1=p2‘;
else
select p1,p2,p3;
end if;

case p3
when ‘a‘ then
select ‘p3 is a‘;
when ‘b‘ then
select ‘p3 is b‘;
when ‘c‘ then
select ‘p3 is c‘;
else
select p3;
end case;

/*條件不滿足會被終止*/
while p1>4
do
set p1=p1-1;
end while;
select p1;

checka:loop
set p1=p1+1;
if p1=14 then
leave checka;
end if;
end loop;
select p1;

/*條件滿足會被終止*/
repeat
set p1=p1-1;
until p1=6
end repeat;
select p1;


END;
$$

set @p_in=3;
set @p_out=2;
set @p_inout=‘b‘;
select ‘check procedure‘ into @p4;

call porc_person_02(@p_in,@p_out,@p_inout);
select @p_in,@p_out,@p_inout,@p4;

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.