应用场景:升级ACCESS数据库到MSSQL数据库,ID自增需要调整为跟ACCESS数据库中的自增字段一样的编号,导致需要不断的添加记录跟删除表重新测试.
技术要点:SQL游标和变量使用。
注意场合:请注意删除前一定要做数据库备份,删除数据后会导致数据库中所有的数据表和表中的记录数据丢失,没备份千万不要尝试,删除数据的后果请读者自负。
操作步骤:请运行脚本前一定要选中要删除的数据库,然后执行下面代码 中的脚本
代码如下:
declare @CurrentTableName nvarchar(100)
declare @CurrentTableObjectID int
declare @deletetableSqlString nvarchar(1000)
--select * from Sys.all_objects where type='U' ;
declare tb cursor local for select name,object_id from Sys.all_objects where type='U' ;
open tb
fetch next from tb into @CurrentTableName,@CurrentTableObjectID
while @@fetch_status=0
begin
set @deletetableSqlString='drop table '+@CurrentTableName
exec sp_executesql @deletetableSqlString;
print '删除数据表'+@CurrentTableName +'完成'
fetch next from tb into @CurrentTableName,@CurrentTableObjectID
end
close tb
deallocate tb
本文详细介绍了如何在数据库迁移过程中调整ID自增字段,并通过SQL游标和变量使用进行表删除操作。确保操作前备份数据库,避免数据丢失。
926

被折叠的 条评论
为什么被折叠?



