MySQL存储过程不按预期进行插入

时间:2013-01-31 15:08:45

标签: mysql select stored-procedures insert

我传递了两个参数,我需要使用多个插入调用填充stats表。它不起作用,任何人都可以帮助我吗?

CREATE PROCEDURE STAT(IN arg_From DATETIME, IN arg_End DATETIME)

INSERT INTO stats(source_id_fk,source_type,stat_type_id_fk,
                 stat_count,modified_on,created_on)   
(SELECT -1 as source_id_fk ,3 as source_type, 1 AS stat_type_id_fk ,
                 COUNT( 1 ) AS stat_count, NOW( ) AS modified_on , 
                 NOW( ) AS created_on 
FROM  wall_posts WHERE post_type =1 
and created_on >= arg_From 
and created_on <= arg_End group by 1,2,3,5,6)

INSERT INTO stats(source_id_fk,source_type,stat_type_id_fk,stat_count,
                 modified_on,created_on) 
(SELECT -1 as source_id_fk ,3 as source_type, 2 AS stat_type_id_fk , 
                 COUNT( 1 ) AS stat_count  , NOW( ) AS modified_on , 
                 NOW( ) AS created_on 
FROM  wall_posts WHERE post_type =2  
and created_on >= arg_From 
and created_on <= arg_End group by 1,2,3,5,6);

1 个答案:

答案 0 :(得分:1)

您需要使用;

结束第一个插入语句

为了让MySQL不认为您的过程已经完成,您必须将这些多个语句封装在BEGIN ... END块中并更改分隔符。

DELIMITER $$
CREATE PROCEDURE STAT(IN arg_From DATETIME, IN arg_eND DATETIME)
BEGIN
INSERT INTO stats(source_id_fk,source_type,stat_type_id_fk,
                 stat_count,modified_on,created_on)   
(SELECT -1 as source_id_fk ,3 as source_type, 1 AS stat_type_id_fk ,
                 COUNT( 1 ) AS stat_count, NOW( ) AS modified_on , 
                 NOW( ) AS created_on 
FROM  wall_posts WHERE post_type =1 
and created_on >= arg_From 
and created_on <= arg_End group by 1,2,3,5,6);

INSERT INTO stats(source_id_fk,source_type,stat_type_id_fk,stat_count,
                 modified_on,created_on) 
(SELECT -1 as source_id_fk ,3 as source_type, 2 AS stat_type_id_fk , 
                 COUNT( 1 ) AS stat_count  , NOW( ) AS modified_on , 
                 NOW( ) AS created_on 
FROM  wall_posts WHERE post_type =2  
and created_on >= arg_From 
and created_on <= arg_End group by 1,2,3,5,6);
END $$
DELIMITER ; /*and change it back to the normal `;` again*/