MySQL将FUNCTION创建为存储过程

时间:2014-07-20 07:21:02

标签: mysql sql stored-procedures

在我的store procedure我希望有简单的函数来返回值。我想让这个Sql命令起作用(这是我的程序中的重复操作):

/*************************************************/
CREATE FUNCTION getDelay( INT id(11) )

    SET @v_is_new := (SELECT COUNT(id) from `tsms_entry_exit` WHERE DATE_FORMAT(`datetime_in`, '%Y-%m-%d') = CURDATE() );

    IF  @v_is_new  IS NULL THEN
           SET @v_delay := SEC_TO_TIME (TIME_TO_SEC(_datetime_in) - TIME_TO_SEC(@v_user_start_time)) ;
            ELSE
                    SET @v_delay :=0;
    END IF;
RETURN @v_delay;
/*************************************************/

我希望将这个功能放到store procedure以下如何进行此操作?

CREATE DEFINER=`root`@`localhost` PROCEDURE `tsmsp_post_entry_exit`(IN `_id` INT(11), IN `_user_id` INT(5), IN `_datetime_in` DATETIME, IN `_datetime_out` DATETIME)
    NO SQL
BEGIN
    DECLARE v_user_start_time INT UNSIGNED;
    DECLARE v_delay           INT(11);
    DECLARE v_pyear           INT UNSIGNED;
    DECLARE v_mounth          INT UNSIGNED;

    SET @v_pyear     = pyear    ( _datetime_in ) ;
    SET @v_mounth  = pmonth( _datetime_in );

    SET @v_user_start_time := (SELECT `start_time` FROM `tsms_range_time` WHERE `user_id` = _user_id);

    IF _id IS NULL THEN
    /* Check for first record today for calculate Delay*/
    SET @v_is_new := (SELECT COUNT(id) from `tsms_entry_exit` WHERE DATE_FORMAT(`datetime_in`, '%Y-%m-%d') = CURDATE() );
    /* calculate USER delay */
    IF  @v_is_new  IS NULL THEN
           SET @v_delay := SEC_TO_TIME (TIME_TO_SEC(_datetime_in) - TIME_TO_SEC(@v_user_start_time)) ;
            ELSE
                    SET @v_delay :=0;
    END IF;    

INSERT INTO `tsms_entry_exit` 
        (
            `user_id`,
            `datetime_in`,
            `datetime_out`,
            `delay`,
            `period`
        )
    VALUES
        (
           _user_id,
           _datetime_in,
           _datetime_out,
           @v_delay,
           CONCAT(@v_pyear, @v_mounth)
        );
        SELECT ROW_COUNT() AS 'Affected rows';
ELSE
    BEGIN
    /* Check for first record today for calculate Delay*/
    SET @v_is_new := (SELECT COUNT(id) from `tsms_entry_exit` WHERE DATE_FORMAT(`datetime_in`, '%Y-%m-%d') = CURDATE() );
    /* calculate USER delay */
    IF  @v_is_new  IS NULL THEN
           SET @v_delay := SEC_TO_TIME (TIME_TO_SEC(_datetime_in) - TIME_TO_SEC(@v_user_start_time)) ;
            ELSE
                    SET @v_delay :=0;
    END IF;
            UPDATE `tsms_entry_exit` SET
                    `user_id`= _user_id,
                        `datetime_in` = _datetime_in,
                        `datetime_out`=_datetime_out,
                        `delay`=_delay,
                        `period`=_period
                 WHERE `id` = _id;
                 SELECT ROW_COUNT() AS 'Affected Update rows';
        END;
END IF;
END

1 个答案:

答案 0 :(得分:0)

您无法在程序中定义函数,但您可以调用该函数。最简单的例子如下所示。

创建你的功能

DELIMITER $$
CREATE FUNCTION getDelay(id int)
returns int
begin
    SET @v_is_new := (SELECT COUNT(id) from `tsms_entry_exit` 
    WHERE DATE_FORMAT(`datetime_in`, '%Y-%m-%d') = CURDATE() );
    IF(@v_is_new  IS NULL) THEN
    SET @v_delay := SEC_TO_TIME (TIME_TO_SEC(_datetime_in) - TIME_TO_SEC(@v_user_start_time)) ;
    ELSE
    SET @v_delay :=0;
    END IF;
RETURN @v_delay;
end $$
DELIMITER ;

然后在你的程序中调用上面定义的函数

DELIMITER $$
CREATE PROCEDURE `tsmsp_post_entry_exit`(IN `_id` INT)
BEGIN
     DECLARE v_delay INT;
    IF _id IS NULL 
    THEN
     select @v_delay := getDelay(_id); <-- Calling Here
    END IF;    
END $$
DELIMITER ;