游客发表

如何查询SQL Server数据库没有主键的表并增加主键

发帖时间:2025-11-04 14:26:53

如何查询SQL Server数据库没有主键的表并增加主键
何查复制declare @tablename sysname    declare @strsql nchar(500)    declare tableNameCursor cursor for   select b.name from sysobjects b where xtype=U and  b.name not in    (select object_name(a.parent_obj)  from sysobjects a where xtype=PK )    open tableNameCursor      fetch next from tableNameCursor into @tablename    while @@FETCH_STATUS = 0 begin   print @tablename    set @strsql= alter table  + @tablename +  add primary key (id)    print @strsql    exec (@strsql)    fetch next from tableNameCursor into @tablename    end   close tableNameCursor    deallocate  tableNameCursor  1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.

    热门排行

    友情链接