0

I have a table that basically looks like this:

ID         Name
 1         test1
 2         test2
 n         testn

I am trying to write a query that first updates 1 row at a time, then if I am feeling greedy, updates all the rows. I tried user-defined variables like this:

SET @x = '1';
SET @name = CONCAT('test', @x);
UPDATE mytable SET Name = @name WHERE ID = @x;

But this query fails. Why is this query failing, and is it possible to improve it to something like a for loop to update every name field in the table? For the latter, is something like this possible?

<for loop>
SET @x = Name;
SET @name = CONCAT('test', @x);
UPDATE mytable SET Name = @name WHERE ID = @x;
<end loop>

1 Answers1

1

Anything wrong with:

  UPDATE mytable
     SET name = CONCAT('test',id)
/* WHERE id = 1 /* If you want to limit it */
Arth
  • 12,789
  • 5
  • 37
  • 69