请问怎样用SQL语句给数据库里面的所有用户表都加一个identity字段?

sammihi2me 2003-05-08 10:22:58
建了表以后发现有时候记录无法删除,看到论坛上说要加identity字段才可以正常,但不知道怎么样用批处理的方式加这个字段
...全文
37 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
8312008 2003-05-08
  • 打赏
  • 举报
回复
楼上是操作库中所有表的,对一个表用
alter table tablename add myID int identity(1,1)
就可以了
yoki 2003-05-08
  • 打赏
  • 举报
回复
declare xcursor cursor for
select name from sysobjects where xtype='u'
declare @name varchar(100)
open xcursor
fetch xcursor into @name
while @@fetch_status=0
begin
exec('alter table '+@name+' add newcol int identity(1,1)')
fetch xcursor into @name
end
close xcursor
deallocate xcursor

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧