I'm trying to write a procedure that when called will update columns in a table with provided information. The procedure will take in the column name to be updated and the value I want inserted into the column. However the table has some columns that are INT and some that are VARCHAR. Is there a way to do this with one procedure or will I need two separate procedures one for a VARCHAR column and one for an INT column?
The example in the title doesn't work and I didn't really expect it to. Just want to know if anyone else has encountered this and found a loophole in MySQL syntax.
CREATE PROCEDURE updateColumn(
IN an_id,
IN a_column_name VARCHAR,
IN a_column_value VARCHAR || INT
)
BEGIN
UPDATE foo
SET a_column_name = a_column_value
WHERE id = id
END $$
I'm trying to write a procedure that when called will update columns in a table with provided information. The procedure will take in the column name to be updated and the value I want inserted into the column. However the table has some columns that are INT and some that are VARCHAR. Is there a way to do this with one procedure or will I need two separate procedures one for a VARCHAR column and one for an INT column?
The example in the title doesn't work and I didn't really expect it to. Just want to know if anyone else has encountered this and found a loophole in MySQL syntax.
CREATE PROCEDURE updateColumn(
IN an_id,
IN a_column_name VARCHAR,
IN a_column_value VARCHAR || INT
)
BEGIN
UPDATE foo
SET a_column_name = a_column_value
WHERE id = id
END $$
Share
Improve this question
edited Feb 7 at 16:04
Barmar
781k56 gold badges545 silver badges659 bronze badges
asked Feb 7 at 15:47
srcourtepattesrcourtepatte
11 bronze badge
New contributor
srcourtepatte is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
|
1 Answer
Reset to default 0here's a trivial example using mysql shell.
DELIMITER $$
CREATE PROCEDURE updateColumn(
IN idFld INT,IN tableName VARCHAR(128),
IN colName VARCHAR(64),IN colValue VARCHAR(255)
)
BEGIN
SET @sql = CONCAT('UPDATE ', tableName, ' SET ', colName, ' = ? WHERE id = ?');
SET @val = colValue;
SET @id = idFld;
PREPARE stmt FROM @sql;
EXECUTE stmt USING @val, @id;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
mysql> describe emp;
+------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | NO | | NULL | |
| last_name | varchar(50) | NO | | NULL | |
| position | varchar(100) | YES | | NULL | |
| salary | decimal(10,2) | YES | | NULL | |
| hire_date | date | YES | | NULL | |
+------------+---------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> select * from emp where ID =1;
+----+------------+-----------+---------------+----------+------------+
| id | first_name | last_name | position | salary | hire_date |
+----+------------+-----------+---------------+----------+------------+
| 1 | Alice | Johnson | Lead Engineer | 82000.00 | 2022-01-15 |
+----+------------+-----------+---------------+----------+------------+
1 row in set (0.00 sec)
mysql> call updateColumn( 1, 'emp', 'position', 'Hardware Designer' );
Query OK, 0 rows affected (0.01 sec)
mysql> call updateColumn( 1, 'emp', 'salary', '92000.00' );
Query OK, 0 rows affected (0.02 sec)
mysql> select * from emp where ID =1;
+----+------------+-----------+-------------------+----------+------------+
| id | first_name | last_name | position | salary | hire_date |
+----+------------+-----------+-------------------+----------+------------+
| 1 | Alice | Johnson | Hardware Designer | 92000.00 | 2022-01-15 |
+----+------------+-----------+-------------------+----------+------------+
1 row in set (0.00 sec)
hope this helps.
SET a_column_name = a_column_value
doesn't work, either. Column names are not evaluated as variables, that tries to set a column that's literally nameda_column_name
. – Barmar Commented Feb 7 at 16:05updateColumnInt()
andupdateColumnChar()
. – Barmar Commented Feb 7 at 16:06WHERE id = id
should beWHERE id = an_id
– Barmar Commented Feb 7 at 16:08BEGIN
/END
when the stored procedure is a single statement. – danblack Commented Feb 8 at 1:23