Monday, January 4, 2010

Dynamic query in MySQL

This script gets all users from localhost in mysql.user table by using dynamic query.

SET @table = 'mysql.user';
SET @host = '''%localhost%''';
SET @s = CONCAT('SELECT * FROM ', @table, ' WHERE host LIKE ', @host);
PREPARE stmt3 FROM @s;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;

No comments:

Post a Comment