SQL批量刪除語句,SQL刪除所有資料

2021-05-28 16:54:20 字數 1454 閱讀 4723

sp_msforeachtable 

'delete from ?

'execute

sp_msforeachtable 

'truncate table ?'

//定義游標

declare tables_cursor cursor

for

select name from sysobjects where type = 'u' //選擇使用者表名

open tables_cursor //開啟游標連線

declare @tablename sysname // 定義變數

fetch next from tables_cursor into @tablename //結果集中一行一行讀取表名

while (@@fetch_status <> -1) //判斷游標狀態 

begin

exec ('trunecate table ' + @tablename) //清空表中的資料

fetch next from tables_cursor into @tablename //下一行資料

end

deallocate tables_cursor //關閉游標

truncate   table   

刪除表中的所有行,而不記錄單個行刪除操作。     

語法   

truncate   table   name   

引數   

name   

是要截斷的表的名稱或要刪除其全部行的表的名稱。   

注釋   

truncate   table   在功能上與不帶   where   子句的   delete   語句相同:二者均刪除表中的全部行。但   truncate   table   比   delete   速度快,且使用的系統和事務日誌資源少。     

delete   語句每次刪除一行,並在事務日誌中為所刪除的每行記錄一項。truncate   table   通過釋放儲存表資料所用的資料頁來刪除資料,並且只在事務日誌中記錄頁的釋放。   

truncate   table   刪除表中的所有行,但表結構及其列、約束、索引等保持不變。新行標識所用的計數值重置為該列的種子。如果想保留標識計數值,請改用   delete。如果要刪除表定義及其資料,請使用   drop   table   語句。   

對於由   foreign   key   約束引用的表,不能使用   truncate   table,而應使用不帶   where   子句的   delete   語句。由於   truncate   table   不記錄在日誌中,所以它不能啟用觸發器。     

truncate   table   不能用於參與了索引檢視的表。   

示例   

下例刪除   authors   表中的所有資料。   

truncate   table   authors

批量刪除表的sql語句

批量刪除表的sql語句,當然這種刪除需要表的名字有相同的字首.declare table nvarchar 300 declare tmpcur cursor for select name from sys.objects where type u and name like n hsupa op...

SQL刪除語句

如果我們要刪除資料庫中表的資料,我們就可以使用delete語句。delete語句的基本語法是 delete from 表名 where 例如,我們想刪除employees表中id 100的記錄,就需要這麼寫 delete from employees where employee id 100 查詢...

SQL刪除語句用法

一 drop 語法 drop table tablename 作用 刪除內容和定義,釋放空間。簡單來說就是把整個表去掉.以後要新增資料是不可能的,除非新增乙個表 二 truncate 語法 truncate table tablename 作用 刪除內容 釋放空間但不刪除定義。與drop不同的是,他...