mysql存储过程使用两个参数插入

时间:2017-06-20 16:16:40

标签: mysql

我是新来的存储过程我将三个字符串传递给过程(table_name,fields_name,values)以插入数据。

DELIMITER 

CREATE PROCEDURE insert_into(IN table_name VARCHAR(255),IN fields_name VARCHAR(255),IN valuess VARCHAR(255),)
    BEGIN
        SET @sql = CONCAT('insert into ',table_name,'(',fields_name,')values (',valuess,')');
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
END
DELIMITER ;

获取错误

#1054 - Unknown column 'table_name' in 'field list'

1 个答案:

答案 0 :(得分:0)

 DELIMITER $$
--
-- Procedures
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_value` (IN `table_name` VARCHAR(1000), IN `fields_name` TEXT, IN `valuess` TEXT)  
BEGIN
 SET @sql = CONCAT('insert into ',table_name,'(',fields_name,')values (',valuess,')');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
  END$$

DELIMITER ;