我试图构建一个mysql存储过程,但似乎有不同的错误,我只是试图从存储过程中运行一个准备好的语句来实现在表中动态插入一些数据。
谁能检查并告诉我我的错误在哪里?
谢谢
DELIMITER $$
CREATE PROCEDURE insert_tracking(IN deviceid VARCHAR(15),IN timing timestamp,IN valid tinyint(1),IN latitude double,IN longitude double,IN speed double,IN course double,IN power double,IN comando varchar(45))
BEGIN
DECLARE query VARCHAR(150);
SET query = "INSERT INTO tracking_? (device_id,time,valid,latitude,longitude,speed,course,power,command) VALUES (?,?,?,?,?,?,?,?,?)";
PREPARE stmt FROM query;
EXECUTE stmt USING deviceid,deviceid,timing,valid,latitude,longitude,speed,course,power,comando;
END $$
我也尝试过这种连接,但它不起作用。
DELIMITER $$
CREATE PROCEDURE insert_tracking(IN deviceid VARCHAR(15),IN timing timestamp,IN valid tinyint(1),IN latitude double,IN longitude double,IN speed double,IN course double,IN power double,IN comando varchar(45))
BEGIN
DECLARE query VARCHAR(200);
SET query = CONCAT('INSERT INTO tracking_',deviceid,' (device_id,time,valid,latitude,longitude,speed,course,power,command) VALUES (?,?,?,?,?,?,?,?,?)');
PREPARE stmt FROM query;
EXECUTE stmt USING deviceid,timing,valid,latitude,longitude,speed,course,power,comando;
DEALLOCATE PREPARE stmt;
END $$
编辑后我有了这个,我测试了 concat 语句,它们分别工作,但准备好的语句一直有问题,它不起作用。
DELIMITER $$
CREATE PROCEDURE insert_tracking(IN deviceid VARCHAR(15),IN timing timestamp,IN valid tinyint(1),IN latitude double,IN longitude double,IN speed double,IN course double,IN power double,IN comando varchar(45))
BEGIN
SET @deviceid = deviceid;
SET @nsert = CONCAT('INSERT INTO tracking_',@deviceid);
SET @query = CONCAT(@nsert,' (device_id,time,valid,latitude,longitude,speed,course,power,command) VALUES ("?","?","?","?","?","?","?","?","?")');
SET @fquery = @query;
PREPARE stmt FROM @fquery;
EXECUTE stmt USING deviceid,timing,valid,latitude,longitude,speed,course,power,comando;
END $$