Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to empty all table records in the database

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to clear all the table records in the database". The content in the article is simple and clear, and it is easy to learn and understand. let's go deep into the editor's train of thought. Let's study and learn how to clear all table records in the database.

1. Search for all the table names and copy the code as a SQL statement: declare @ trun_name varchar (8000) set @ trun_name='' select @ trun_name=@trun_name + 'truncate table' + [name] + 'from sysobjects where xtype='U' and status > 0 exec (@ trun_name)

This method is suitable for situations where there are not too many tables, otherwise the number of tables is too large, which exceeds the length of the string and cannot be cleaned completely. two。 Use cursors to clean up all tables and copy the code as follows: declare @ trun_name varchar (50) declare name_cursor cursor for select 'truncate table' + name from sysobjects where xtype='U' and status > 0 open name_cursor fetch next from name_cursor into @ trun_name while @ @ FETCH_STATUS = 0 begin exec (@ trun_name) print 'truncated table' + @ trun_name fetch next from name_cursor into @ trun_name end close name_cursor deallocate name_cursor

This is my own construction, can be used as a stored procedure call, can empty all the table data at once, and can also selectively empty the table. 3. Copy the code using Microsoft's undisclosed stored procedure as follows: exec sp_msforeachtable "truncate table?"

This method can empty all tables at once, but can not add filter conditions.

Thank you for your reading. the above is the content of "how to clear all the table records in the database". After the study of this article, I believe you have a deeper understanding of how to empty all the table records in the database. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report