Am updating my sql table using while loops , In my table records “id” are in this order 27,28,29,30 upto 45. am trying to update thise “id” into 2,3,4,5 upto 16 i used this loop but am getting error only
DECLARE @a INT
DECLARE @b INT
SET @a = 2
SET @b = 27
WHILE @b < 42
BEGIN
exec sp_executesql 'UPDATE Cpart2_TEST_2 SET id = @a where id = @b
SET @a = @a + 1
SET @b = @b + 1
END
This is the error message
1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to
use near ‘DECLARE @a INT
DECLARE @b INT
SET @a = 2
SET @b = 27
WHILE @b < 42
BEGIN
' at line 1
Change your exec line to:
I haven’t parsed this so it may have syntax errors. But get that working, and you should be good.
One thing to consider… if ‘id’ is an IDENTITY column, you will probably not be able to alter it.