我正在调用下面的存储过程,其中表名是根据当前日期生成的:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `sp_pickalertstatus`$$
CREATE PROCEDURE `sp_pickalertstatus`()
BEGIN
DECLARE msgID BIGINT (10);
DECLARE tbl VARCHAR(100);
SET msgID=(SELECT alertID FROM tbl_intofone_alert_transaction WHERE
deliverystatus LIKE '22%' LIMIT 1);
IF(msgID IS NOT NULL)THEN
SET tbl=(SELECT CONCAT('history.tbl_',DATE_FORMAT(NOW(),"%y%m%d")));
INSERT INTO tbl (MEMBERID,SOURCE,MOBILE,SMPPID,PRIORITY,SMSID
,SMSCONTENTTYPE,MESSAGE,REQUESTDATETIME,DR_GROUPID,dr_service_type,
dr_celleb_msg_id,dr_submit_date,dr_done_date,dr_stat,dr_err,dr_smppid)
SELECT memberid,SenderID,MSISDN,SMPPID,'1',alertID,'TXT',Message,
updatetime,'0','PIN','0',DATE_FORMAT(InTime,'ymdHis'),
DATE_FORMAT(updatetime,'ymdHis'),'DELIVRD',
'000','03' FROM tbl_intofone_alert_transaction WHERE alertID=msgID;
END IF;
END$$
DELIMITER ;
我收到错误“错误代码:1146 表'test.tbl'不存在”。我在这里做错了什么?
在建议使用 Prepared 语句执行此操作后,我更改了上面的代码:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `sp_pickalertstatus22`$$
CREATE PROCEDURE `sp_pickalertstatus22`()
BEGIN
DECLARE msgID BIGINT (10);
DECLARE tbl VARCHAR(100);
SET msgID=(SELECT alertID FROM tbl_intofone_alert_transaction WHERE deliverystatus LIKE '22%' LIMIT 1);
IF(msgID IS NOT NULL)THEN
SET tbl=(SELECT CONCAT('history.tbl_',DATE_FORMAT(NOW(),"%y%m%d")));
SET @sl=CONCAT('INSERT INTO ?',' (MEMBERID,SOURCE,MOBILE,SMPPID,PRIORITY,SMSID,SMSCONTENTTYPE,MESSAGE,REQUESTDATETIME,
DR_GROUPID,dr_service_type,dr_celleb_msg_id,dr_submit_date,dr_done_date,dr_stat,dr_err,dr_smppid)
SELECT memberid,SenderID,MSISDN,SMPPID,?,alertID,?,Message,updatetime,?,?,?,DATE_FORMAT(InTime,?),
DATE_FORMAT(updatetime,?),?,?,? FROM tbl_intofone_alert_transaction WHERE alertID=?');
PREPARE stmt FROM @sl;
SET @tbl=tbl;
SET @pr='06';
SET @txt='TXT';
SET @grp='0';
SET @srtyp='PIN';
SET @clmsg='0';
SET @intm='%y%m%d%H%i%s';
SET @outm='%y%m%d%H%i%s';
SET @drst='DELIVRD';
SET @drerr='000';
SET @drsmp='03';
SET @msg=msgID;
EXECUTE stmt USING @tbl,@pr,@txt,@grp,@srtyp,@clmsg,@intm,@outm,@drst,@drerr,@drsmp,@msg;
END IF;
END$$
DELIMITER ;
但它仍然不起作用?并得到一个错误
Error Code : 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax
to use near '? MEMBERID,SOURCE,MOBILE,SMPPID,PRIORITY,SMSID,
SMSCONTENTTYPE,MESSAGE,REQUESTDA' at line 1
在使用查询字符串执行准备语句时:
EXECUTE stmt USING '06','TXT','0','PIN','0','%y%m%d%H%i%s'
,'%y%m%d%H%i%s','DELIVRD','000','03';
得到错误:
Error Code : 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to
use near ''06','TXT','0','PIN','0','%y%m%d%H%i%s','%y%m%d%H%i%s','DELIVRD',
'000','03';' at line 25