2

I am trying to reset the auto increment value in one of my tables based on the number of rows currently in it. Here is the code I have so far.

SET @numrows = 0;

SELECT COUNT(*) total, @numrows := COUNT(*) + 1 numrows FROM maj_user ;
ALTER TABLE `maj_user` AUTO_INCREMENT = @numrows ;

This works great if I execute it in MySQL Workbench. However, I need to save this as an SQL file and execute it as part of a database import script. If I do this, I get this:

ERROR 1064 (42000) at line 39: 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 '@numrows' at line 1

Line 39 is the ALTER TABLE statement. Any ideas?

4

1 に答える 1

0

@numrows の実際の設定をスキップするように構文を変更できますか? 何が問題なのかわかりませんが、回避策は次のようになります。

ALTER TABLE `maj_user` AUTO_INCREMENT = (SELECT COUNT(*) + 1 from maj_user);
于 2013-04-01T19:31:25.450 に答える