开发者

Create a stored procedure to iterate through a list of tables and truncate them in MySQL

开发者 https://www.devze.com 2023-02-07 08:20 出处:网络
I\'m debating whether or not to try running t开发者_JAVA技巧hrough a list of tables and truncating them with a stored procedure. Would it be that easy with MySql and how would I do it?The main piece o

I'm debating whether or not to try running t开发者_JAVA技巧hrough a list of tables and truncating them with a stored procedure. Would it be that easy with MySql and how would I do it?


The main piece of info you need is the list of tables. Most platforms support this:

select table_name from information_schema.tables

However, before you code the sproc, do a select * from information_schema.tables and examine the entries, there may be some you do not expect -- system tables and such, so you may need to craft a filter to get the set you want.

Since I don't do mySQL that much, I can't show you the code, but if you can translate this from MS SQL, and fill in some blanks you can make it work:

declare @table_name varchar(200)
while 1=1 begin
    select top 1 @table_name = table_name
      from information_schema.tables
     where ....possible filter...
    if @table_name is null break

    -- for this line you may need dynamic sql
    truncate table @table_name
end    
0

精彩评论

暂无评论...
验证码 换一张
取 消