欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

深入mysql存储过程中表名使用参数传入的详解

程序员文章站 2024-02-22 10:17:40
begin declare date_str varchar(8);declare table_prefix varchar(20);set table_prefix='...

begin

declare date_str varchar(8);
declare table_prefix varchar(20);
set table_prefix='mail_rare_visit_';
set date_str = date_format(current_date(),'%y%m%d');
set @table_name = concat(table_prefix, date_str);
set @s = concat('create table ', @table_name, '(
  `id` int(11) not null auto_increment,
  `user_id` int(11) not null,
  `city_id` int(11) not null,
  `email` varchar(300) not null,
  `channel_id` int(11) default null,
  `w_revenue` decimal(7,6) not null default  0.000000,
  `w_buy` decimal(7,6) not null default 0.000000,
  `w_browse` decimal(7,6) not null default 0.000000,
  `rec_goods` varchar(800) default null,
  `update_time` timestamp not null default current_timestamp on update current_timestamp,
  primary key (`id`),
  key `idx_user_id` (`user_id`),
  key `idx_city_id` (`user_id`),
  key `idx_email` (`email`),
  key `idx_channel_id` (`channel_id`)
) engine=myisam default charset=utf8;');

prepare stmt from @s;
execute stmt;

deallocate prepare stmt;

end