工作日创建过程错误()

时间:2017-09-05 06:50:25

标签: mysql stored-procedures procedure weekday

我只需要在星期一执行程序;所以我首先在WEEKDAY()上做一个测试,但是有一个语法错误,我找不到有什么问题?

CREATE DEFINER=`root`@`localhost` PROCEDURE `fm_Upd_Histo_Inv`()
BEGIN
-- Test if it is Monday 
    CASE WEEKDAY(curdate()) = 0 then
        insert into db1w_histo_inventory (year, week, store, total, to_do)
            select year(curdate()),
                WEEKOFYEAR(curdate()),
                S.store, 
                count(S.INVDATE) as TotalToDo,
                sum(datediff(curdate(), S.INVDATE) > '365') as 'TO_DO'
            from mrqr_stock S
            left join mrqr_organisms O
                on O.ORGANISM = S.STORE
            where (O.ORGANISM like '01%'
                        or O.ORGANISM like 'VV%'
                        or O.ORGANISM like 'IK%')
            group by S.STORE
    end;
END

1 个答案:

答案 0 :(得分:0)

CASE通常用于select,insert,update,delete等语句中。它不用于控制流程。请改用IF。并设置不同的分隔符。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `fm_Upd_Histo_Inv`()
BEGIN
-- Test if it is Monday 
    IF WEEKDAY(curdate()) = 0 then
        insert into db1w_histo_inventory (year, week, store, total, to_do)
            select year(curdate()),
                WEEKOFYEAR(curdate()),
                S.store, 
                count(S.INVDATE) as TotalToDo,
                sum(datediff(curdate(), S.INVDATE) > '365') as 'TO_DO'
            from mrqr_stock S
            left join mrqr_organisms O
                on O.ORGANISM = S.STORE
            where (O.ORGANISM like '01%'
                        or O.ORGANISM like 'VV%'
                        or O.ORGANISM like 'IK%')
            group by S.STORE
    end if;
END $$
DELIMITER ;