-1

我正在传递两个参数,我需要使用多个插入调用填充统计表。它不起作用,有人可以帮我解决这个问题吗?

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);
4

1 回答 1

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*/
于 2013-01-31T15:47:23.513 回答