问题
I have a stored procedure that writes results to a Result table. How do you truncate / erase a table from within a stored procedure?
example
call peformTest()
truncate TestResultTable;
//do stuff with new data to insert into TestResultTable
end
回答1:
If you want to remove all data from the table, then your syntax is correct:
truncate testResultTable;
or
truncate table testResultTable;
Depending on your specific needs, if you need to get rid of the table correctly and then re-create it, you can do:
drop table testResultTable;
create table testResultTable as select ... from ... where ...
回答2:
Not sure if there is any difference in SQL vs stored procedure in the way they execute. But usually the format for truncating is: Truncate Table tableName;
here is the reference: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
回答3:
In the stored procedure simple:
DELETE FROM table WHERE 1 = 1
Other example in PL/SQL:
PL/SQL SP Truncate
Or solution by @Churk.
来源:https://stackoverflow.com/questions/9470398/mysql-how-do-you-truncate-a-table-in-a-stored-procedure