[英]How to insert data to table by store procedure with auto-incremented column
我有一个存储过程如下
DROP PROCEDURE IF EXISTS maintain//
CREATE PROCEDURE maintain
(
IN inMaintainType CHAR(1), -- 'i' = Insert, 'u'= Update/Edit, 'd'= Delete
IN inEntityId INT, -- 0 for Insert Case
IN inEntityName VARCHAR(100),
IN inEntityDescription VARCHAR(100),
IN inEntityPrefix CHAR(1),
IN inStatus CHAR(1), -- 'a' = Active, 'i' = Not active
IN inEmpId INT,
OUT outReturnStatus INT,
OUT outReturnRemarks VARCHAR(100)
)
BEGIN
IF inMaintainType= 'i'
THEN
INSERT INTO Entity
(
EntityId,
EntityName,
EntityDescription,
EntityPrefix,
Status,
CreatedBy,
CreatedDate,
ModifiedBy,
ModifiedDate
)
VALUES
(
li_EntityId,
inEntityName,
inEntityDescription,
inEntityPrefix,
'a',
inEmpId,
now(),
inEmpId,
now()
);
if row_count() != 0
THEN SET outReturnStatus =0 ,
outReturnRemarks = 'Insert Successful';
ELSE SET outReturnStatus = 1,
outReturnRemarks = 'Insert Not Successful';
END IF;
END IF ;
我想调用该过程以使用变量插入数据
mysql_query("CALL maintain('i','$EntityId','$EntityName','$EntityDescription','$EntityPrefix','$Status','$EmpId',@outvari1,@outvari2)")or die(mysql_error());
但这向我显示了错误
“字段列表”中的未知列“ li_EntityId”
EntityId是自动递增的字段。
要将以上评论澄清为实际答案...
您已经设置了表格,以便EntityId字段将自动递增。
因此,当您将记录插入表中时,无需显式为ID字段添加值-它将为您完成。
因此,解决方案是从INSERT INTO ...语句中删除EntityId字段,并从插入的值中删除li_EntityId值,因此仅将8个参数传递到其余8个字段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.