简体   繁体   English

如何使用自动增加的列按存储过程将数据插入表

[英]How to insert data to table by store procedure with auto-incremented column

I have a stored procedure as below 我有一个存储过程如下

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 ;

I want to call the procedure to insert data with variables 我想调用该过程以使用变量插入数据

mysql_query("CALL maintain('i','$EntityId','$EntityName','$EntityDescription','$EntityPrefix','$Status','$EmpId',@outvari1,@outvari2)")or die(mysql_error());

But it's showing me the error 但这向我显示了错误

Unknown column 'li_EntityId' in 'field list' “字段列表”中的未知列“ li_EntityId”

EntityId is the auto incremented field. EntityId是自动递增的字段。

To clarify the comments above into an actual answer... 要将以上评论澄清为实际答案...

You have set up your table so that the field EntityId will Auto-Increment. 您已经设置了表格,以便EntityId字段将自动递增。

Because of this when you insert a record into the table you don't need to explicitly add a value for the ID field - it will do it for you. 因此,当您将记录插入表中时,无需显式为ID字段添加值-它将为您完成。

The solution is therefore to remove the EntityId field from the INSERT INTO ... statement, and to remove the li_EntityId value from the inserted values, so only 8 arguments are passed into the remaining 8 fields. 因此,解决方案是从INSERT INTO ...语句中删除EntityId字段,并从插入的值中删除li_EntityId值,因此仅将8个参数传递到其余8个字段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM