游標(biāo)刪除多個表里臟數(shù)據(jù)的方法
第一種方法:
CREATE proc [dbo].[delAllRecord]
as
declare @tableName nvarchar(255)
declare @Sql nvarchar(255)
Declare curTable Cursor
for select Table_Name from information_schema.tables where TABLE_TYPE='BASE TABLE'
Open curTable
Fetch Next From curTable Into @tableName
WHILE(@@FETCH_STATUS = 0)
BEGIN
set @Sql = N'delete from '+@tableName
exec sp_executesql @sql
Fetch Next From curTable Into @tableName
end
CLOSE curTable
DEALLOCATE curTable
第二種方法:
--declare test_cursor cursor scroll for
--select id,table_name from dbo.section_type
--open test_cursor
--declare @id int
--declare @table_name nvarchar(50)
--while @@fetch_status=0
--begin
--fetch next from test_cursor into @id,@table_name
--print @id
--print @table_name
--end
--close test_cursor
--deallocate test_cursor
--刪除projectrangtree的臟數(shù)據(jù)
delete from projectrangtree where deleteversion>0
delete from projectrangtree where type=3 and parentid not in(select id from projectrangtree where type=2)
delete from projectrangtree where type=4 and parentid not in(select id from projectrangtree where type=3)
delete from projectrangtree where type=5 and parentid not in(select id from projectrangtree where type=4)
--刪除section_settings的臟數(shù)據(jù)
delete from section_settings where parent_prj_tree_id not in(select id from projectrangtree)
--刪除各個表里的測點
declare @table_name varchar(50)
declare @sql nvarchar(500)--此處要注意,聲明的長度一定要夠
--declare @measuring_point_id nvarchar(500)
declare del_cursor cursor scroll for
select table_name from section_type
open del_cursor
fetch next from del_cursor into @table_name
--print @table_name
while (@@fetch_status=0)
begin
--print quotename(@table_name)
--set @measuring_point_id='select measuring_point_id from '+quotename(@table_name)
--exec sp_executesql @measuring_point_id
set @sql = 'delete from '+ quotename(@table_name) +' where measuring_point_id not in(select id from measuring_point_setting)'
exec sp_executesql @sql
--delete from @table_name where measuring_point_id not in (select id from measuring_point_setting)
fetch next from del_cursor into @table_name
end
close del_cursor
deallocate del_cursor
--delete from (select talbe_name from section_type) where measuring_point_id not in (select id from measuring_point_setting)
- SQL Server游標(biāo)的使用/關(guān)閉/釋放/優(yōu)化小結(jié)
- SQL Server遍歷表中記錄的2種方法(使用表變量和游標(biāo))
- sqlserver中觸發(fā)器+游標(biāo)操作實現(xiàn)
- 教你怎么使用sql游標(biāo)實例分享
- SQL server使用自定義函數(shù)以及游標(biāo)
- sql 游標(biāo)使用筆記
- SQL Server的通用分頁存儲過程 未使用游標(biāo),速度更快!
- sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板
- MSSQL 游標(biāo)使用 心得
- sqlserver 游標(biāo)的簡單示例
- SQL Server 游標(biāo)語句 聲明/打開/循環(huán)實例
相關(guān)文章
sql server創(chuàng)建臨時表的兩種寫法和刪除臨時表
這篇文章主要介紹了sql server創(chuàng)建臨時表的兩種寫法和刪除臨時表 ,需要的朋友可以參考下2015-07-07學(xué)會sql數(shù)據(jù)庫關(guān)系圖(Petshop)
花了這么多時間最終還是回到了數(shù)據(jù)庫,但是數(shù)據(jù)庫中一張一張的表格找不到腳本,也不是自己設(shè)計的數(shù)據(jù)庫,完全沒有一點頭緒,后來突然想起來sql有個數(shù)據(jù)庫關(guān)系圖,可以很快的適合數(shù)據(jù)庫程序員很快的掌握數(shù)據(jù)庫表之間的關(guān)系2012-08-08sqlserver 各種判斷是否存在(表名、函數(shù)、存儲過程等)
在sql server中,如何判斷sql server表是否存在呢?下面就將為您詳細(xì)介紹該方法,供您參考,希望對您加深理解sql server表能起到些許作用2013-02-02Sql學(xué)習(xí)第一天——SQL 將變量定義為Table類型(虛擬表)
sql語句中的變量時通常我們定義的都是像char,varchar,nvarchar之類的,接下來教大家實現(xiàn)讓變量作為一個像虛擬表一樣,感性的各位可以參考下哈2013-03-03SQL Server 2005 數(shù)據(jù)庫轉(zhuǎn) SQL Server 2000的方法小結(jié)
這篇文章主要介紹了SQL Server 2005 數(shù)據(jù)庫轉(zhuǎn) SQL Server 2000的方法,需要的朋友可以參考下2014-04-04SQL Server誤區(qū)30日談 第12天 TempDB的文件數(shù)和需要和CPU數(shù)目保持一致
TempDB的文件沒有必要分布在多個存儲器之間。如果你看到PAGELATCH類型的等待,即使你進行了分布也不會改善性能,而如果PAGEIOLATCH型的等待,或許你需要多個存儲器,但這也不是必然-有可能你需要講整個TempDB遷移到另一個存儲系統(tǒng),而不是僅僅為TempDB增加一個文件2013-01-01