Mysql 存储过程小例子

创建存储过程:

DELIMITER $$
USE `database_name`$$
DROP PROCEDURE IF EXISTS `add_or_update_user`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_or_update_user`(IN `id` INT, IN `username` VARCHAR(50))
BEGIN
    INSERT INTO USER(`id`,`username`,`created`)
        VALUES(id,username,UNIX_TIMESTAMP(NOW()))
        ON DUPLICATE KEY UPDATE 
            id=VALUES(id),
            username=VALUES(username),
            created=VALUES(created);
    SELECT * FROM USER WHERE id=id;
END$$
DELIMITER ;

调用存储过程:

CALL `add_or_update_user`(1, "username");

 

posted on 2016-07-21 15:14  Jacky Yu  阅读(313)  评论(0编辑  收藏  举报