修改主键属性

declare cur_table Cursor local FORWARD_ONLY For 
Select so.name Table_name,                   --表名字
       sc.name Iden_Column_name,             --自增字段名字
       ident_current(so.name) curr_value,    --自增字段当前值
       ident_incr(so.name) incr_value,       --自增字段增长值
       ident_seed(so.name) seed_value        --自增字段种子值
  from sysobjects so ,syscolumns sc where  so.id = sc.id and columnproperty(sc.id, sc.name, 'IsIdentity') = 1  and so.XTYPE='U'
  
open cur_table
 ----声明变量
    declare @tableId numeric(18, 0)--表中目前的种子
declare @columnName nvarchar(500)--自增列名
declare @tableSeed numeric(18, 0)--自增字段种子值
declare @tableName nvarchar(500)--表名
declare @incr_value int         --步长
declare @tableSeedSQL nvarchar(1000)--动态语句
declare @prams nvarchar(1000)--参数
set @prams='@p  numeric(18, 0) output,@tableId numeric(18, 0)'
fetch next from cur_table into @tableName,@columnName, @tableId,@incr_value, @tableSeed
while @@FETCH_STATUS=0
begin
        set @tableSeed=0
--声明变量,查询大于种子的偶数
set @tableSeedSQL='select @p= MAX('+@columnName+') from '+quotename(@tableName)+' where '+@columnName+'%2=0 and '+@columnName+' >@tableId'
exec sp_executesql @tableSeedSQL,@prams,@p=@tableSeed output,@tableId=@tableId
 
            --如果没有大于种子的偶数,目标种子为当前种子
if(@tableSeed is null or @tableSeed=0)
set @tableSeed=@tableId
--设置目标种子为偶数
if(@tableSeed%2=1)
set @tableSeed=@tableSeed+1
   DBCC CHECKIDENT(@tableName,RESEED,@tableSeed)
    fetch next from cur_table into @tableName,@columnName, @tableId,@incr_value, @tableSeed
         end
close cur_table
deallocate cur_table

转载于:https://my.oschina.net/baobao/blog/14515

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值