0

我想写一个这样的程序:

CREATE PROCEDURE `addByType`(IN myDate DATETIME, IN myType INT, OUT output DATETIME)
BEGIN
DECLARE unit ?;
CASE  
    WHEN myType = 1 THEN 
        SET unit = HOUR;        
    WHEN myType = 2 THEN
        SET unit = DAY;
    ...
END CASE;
SELECT TIMESTAMPADD(unit, 1, myDate) INTO output;
END

有可能吗?我可以将单位关键字存储到变量中吗?或者还有其他方法可以达到同样的效果吗?

4

1 回答 1

1

请试试这个:

CREATE PROCEDURE `addByType`(IN myDate DATETIME, IN myType INT, OUT output DATETIME)
BEGIN

CASE  
    WHEN myType = 1 THEN 
        SET @unit = 'HOUR';        
    WHEN myType = 2 THEN
        SET @unit = 'DAY';
END CASE;

SET @sql = CONCAT('SELECT TIMESTAMPADD(', @unit, ', 1, \'', myDate, '\') INTO @output;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET output = @output;

END

可能包括语法错误,必须查找某些内容。但我相信你能弄清楚。得走了,明天再去看看。

PS:这是准备好的语句的手动输入

于 2013-02-27T17:28:23.823 回答