MySQL how do you truncate a table in a stored procedure?

落爺英雄遲暮 提交于 2019-12-24 10:15:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!