主页 > 知识库 > 游标删除多个表里脏数据的方法

游标删除多个表里脏数据的方法

热门标签:庆阳地图标注 电销外呼系统软件功能 榕城市地图标注 美团地图标注商户认证注册 浙江稳定外呼系统供应商 北京400电话办理多少钱 承德地图标注公司名需要花钱吗 咸阳电脑外呼系统运营商 怎么给高德做地图标注

第一种方法:

复制代码 代码如下:

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的脏数据

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的脏数据

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游标的使用/关闭/释放/优化小结
  • SQL Server遍历表中记录的2种方法(使用表变量和游标)
  • sqlserver中触发器+游标操作实现
  • 教你怎么使用sql游标实例分享
  • SQL server使用自定义函数以及游标
  • sql 游标使用笔记
  • SQL Server的通用分页存储过程 未使用游标,速度更快!
  • sqlserver 函数、存储过程、游标与事务模板
  • MSSQL 游标使用 心得
  • sqlserver 游标的简单示例
  • SQL Server 游标语句 声明/打开/循环实例

标签:新乡 重庆 上海 昭通 呼和浩特 江苏 贵州 拉萨

巨人网络通讯声明:本文标题《游标删除多个表里脏数据的方法》,本文关键词  游标,删除,多个,表里,脏,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《游标删除多个表里脏数据的方法》相关的同类信息!
  • 本页收集关于游标删除多个表里脏数据的方法的相关信息资讯供网民参考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推荐文章