mysql存储过程 动态SQL

建表语句:

CREATE TABLE `t_user_log` (

`userID` int(11) unsigned DEFAULT NULL,

`userName` varchar(40) DEFAULT NULL,

`oprDate` datetime DEFAULT NULL,

`oprContent` varchar(80) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8

/*!50100 PARTITION BY RANGE (year(oprDate))

(PARTITION part0 VALUES LESS THAN (201310) ENGINE = MyISAM,

PARTITION part1 VALUES LESS THAN (201311) ENGINE = MyISAM,

PARTITION part2 VALUES LESS THAN (201312) ENGINE = MyISAM,

PARTITION part3 VALUES LESS THAN (201401) ENGINE = MyISAM,

PARTITION part4 VALUES LESS THAN (201402) ENGINE = MyISAM,

PARTITION part5 VALUES LESS THAN (201403) ENGINE = MyISAM)


存储过程语句:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_batchAddUser`(in userName varchar(4000), in oprDate varchar(4000),in oprContent varchar(4000))
BEGIN
-- 定义变量
declare tmp_userName varchar(4000);
declare tmp_oprDate varchar(4000);
declare tmp_oprContent varchar(4000);
declare v_userName varchar(20);
declare v_oprDate varchar(20);
declare d_oprDate datetime;
declare v_oprContent varchar(20);
declare i_userName_num int;
declare i_oprDate_num int;
declare i_oprContent_num int;
declare v_sql varchar(6000);

set v_sql = 'insert into t_user_log(userID, userName, oprDate, oprContent) values(';
set tmp_userName = userName;
set tmp_oprDate = oprDate;
set tmp_oprContent = oprContent;

set i_userName_num = instr(tmp_userName, '|');
set i_oprDate_num = instr(tmp_oprDate, '|');
set i_oprContent_num = instr(tmp_oprContent, '|');

-- select concat(SUBSTRING(tmp_userName, 1, 5), ' ', SUBSTRING(tmp_userName, 7));

while (i_userName_num > 0 and tmp_userName is not null) do

set v_userName = substring(tmp_userName, 1, i_userName_num - 1);
set v_oprDate = subString(tmp_oprDate, 1, i_oprDate_num - 1);
set v_oprContent = subString(tmp_oprContent, 1, i_oprContent_num - 1);
set d_oprDate = str_to_date(v_oprDate, '%Y-%m-%d %H:%i:%s');

set tmp_userName = substring(tmp_userName, i_userName_num + 1);
set tmp_oprDate = substring(tmp_oprDate, i_oprDate_num + 1);
set tmp_oprContent = substring(tmp_oprContent, i_oprContent_num + 1);

set i_userName_num = instr(tmp_userName, '|');
set i_oprDate_num = instr(tmp_oprDate, '|');
set i_oprContent_num = instr(tmp_oprContent, '|');

select concat(v_userName, ' ', d_oprDate, ' ', v_oprContent);
SET v_sql = CONCAT(v_sql, i_userName_num, ',', '\"',v_userName,'\"', ',', '\"',d_oprDate, '\"', ',', '\"',v_oprContent,'\"', '),(');
end while;

select concat(tmp_userName, ' ', tmp_oprDate, ' ', tmp_oprContent);
SET v_sql = CONCAT(v_sql, i_userName_num, ',', '\"', tmp_userName,'\"', ',', '\"',tmp_oprDate,'\"', ',', '\"',tmp_oprContent,'\"', ');');
select concat(v_sql);

-- 执行动态语句
set @v_sql = v_sql;
prepare stmt1 from @v_sql;
execute stmt1;
deallocate prepare stmt1;

END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值