I have a Java
application that can send SQL
queries to a MySQL
server.
I need to be able to truncate all tables form a specified database. So far I can retrieve the queries I need via a select statement, but I can't find a way to execute it in one single SQL
statement. I can't know the schema beforehand because it constantly changes depending on the user.
I have searched over StackOverflow but there is not a solution compliant to my problem or they don't work.
NOT THROUGH THE MYSQL API, ONLY SQL. I KNOW THERE ARE SIMILAR QUESTIONS BUT THEY DON'T WORK OR USE STORED PROCEDURES. I REQUIRE PLAIN SQL COMPATIBLE WITH MYSQL.
This is the SQL
script I have so far:
SELECT CONCAT(
'SET FOREIGN_KEY_CHECKS=0; ',
GROUP_CONCAT(dropTableSql SEPARATOR '; '), '; ',
'SET FOREIGN_KEY_CHECKS=1;'
) as dropAllTablesSql
FROM ( SELECT Concat('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME) AS dropTableSql
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'testschema' ) as queries