SQL Server2008批量刪除數(shù)據(jù)的方法
作者:佚名
SQL Server2008中應(yīng)該如何批量刪除呢?這是很多人都提到過(guò)的問(wèn)題。下文對(duì)該問(wèn)題的處理方法作了詳盡的闡述,希望對(duì)您能有所啟迪。
SQL Server2008批量刪除數(shù)據(jù)使我們?cè)谑褂?a >SQL Server2008數(shù)據(jù)庫(kù)時(shí)經(jīng)常要用到的操作,下面就為您介紹SQL Server2008批量刪除數(shù)據(jù)的方法,供您參考。
- -- 測(cè)試數(shù)據(jù)
- create table dbo.temptb_1 (id int);
- create table guest.temptb_2 (id int);
- -- 打開(kāi)隱式事務(wù)
- SET IMPLICIT_TRANSACTIONS ON
- -- 執(zhí)行動(dòng)態(tài)刪除
- --定義一個(gè)變量
- declare @sql varchar(max);
- set @sql='';
- select @sql=@sql+'drop table '+
- --獲取表名稱,形如:dbo.temptb_***,escape '\'表示'\'為轉(zhuǎn)義符號(hào)
- QUOTENAME(SCHEMA_NAME([schema_id]))+'.'+QUOTENAME([name])+';'
- from sys.tables where where is_ms_shipped =0 and [name] like 'temptb\_%' escape '\'
- -- select @sql;
- EXEC(@sql);
- -- 檢查刪除是否正確
- select * from sys.tables
- -- 正確,則提交事務(wù),確認(rèn)刪除
- commit tran
- -- 不正確,則回滾事務(wù),取消刪除
- rollback tran
- -- 關(guān)閉隱式事務(wù)
- SET IMPLICIT_TRANSACTIONS OFF
【編輯推薦】
責(zé)任編輯:段燃
來(lái)源:
互聯(lián)網(wǎng)