Mysql自訂函數

來源:互聯網
上載者:User

標籤:

1.mysql自訂函數
delimiter $$create function f1(    i1 int,    i2 int)returns intBEGIN    declare num int;    set num = i1 + i2;    return(num);END $$delimiter ;
2.刪除函數
drop function func_name;
3、執行函數
# 擷取傳回值declare @i VARCHAR(32);select UPPER(‘alex‘) into @i;SELECT @i;# 在查詢中使用select f1(11,nid) ,name from tb2;
4.mysql 條件陳述式

1.if 條件陳述式

delimiter $$CREATE PROCEDURE proc_if ()BEGIN        declare i int default 0;    if i = 1 THEN        SELECT 1;    ELSEIF i = 2 THEN        SELECT 2;    ELSE        SELECT 7;    END IF;END$$delimiter ;

2.迴圈語句

  1)。while迴圈

delimiter \CREATE PROCEDURE proc_while ()BEGIN    DECLARE num INT ;    SET num = 0 ;    WHILE num < 10 DO        SELECT            num ;        SET num = num + 1 ;    END WHILE ;END\delimiter ;

  2)。repeat迴圈

delimiter \CREATE PROCEDURE proc_repeat ()BEGIN    DECLARE i INT ;    SET i = 0 ;    repeat        select i;        set i = i + 1;        until i >= 5    end repeat;END\delimiter ;

  3)。loop

delimiter \CREATE PROCEDURE proc_loop ()BEGIN        declare i int default 0;    loop_label: loop        select i;        set i=i+1;        if i>=5 then            leave loop_label;            end if;    end loop;END\delimiter ;

3、動態執行SQL語句

delimiter \DROP PROCEDURE IF EXISTS proc_sql \CREATE PROCEDURE proc_sql ()BEGIN    declare p1 int;    set p1 = 11;    set @p1 = p1;    PREPARE prod FROM ‘select * from tb2 where nid > ?‘;    EXECUTE prod USING @p1;    DEALLOCATE prepare prod; END\delimiter ;

 

 

 

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.