Passing FieldName as Parameter in MySQL Stored Procedure

后端 未结 1 1729
独厮守ぢ
独厮守ぢ 2020-12-19 09:55

I am passing table field name as parameter to stored procedure but stored procedure take field name as value instead of field name and throws error.

e.g if i pass va

相关标签:
1条回答
  • 2020-12-19 10:51

    You can use prepared statements, for example -

    CREATE `VSK_Comments_UpdateAction`(IN FieldName varchar(30),IN FieldValue tinyint,CID bigint)
    BEGIN
      SET @query = CONCAT('Update comments Set ', FieldName, '=? WHERE commentid=?');
      PREPARE stmt FROM @query;
      SET @FieldValue = FieldValue;
      SET @CID = CID;
      EXECUTE stmt USING @FieldValue, @CID;
      DEALLOCATE PREPARE stmt;
    END;
    
    0 讨论(0)
提交回复
热议问题