zoukankan      html  css  js  c++  java
  • 将sql Server 的table的列 ,由非自增长改为自增长

    转载:http://www.thinksaas.cn/topics/0/423/423869.html

    Demo

    /**************** 准备环境********************/
    
    --判断是否存在test表
    if object_id(N'test',N'U') is not null
    drop table test
    
    --创建test表
    create table test
    (
    id int not null,
    name varchar(20) not null
    )
    
    --插入临时数据
    insert into test values (1,'成龙')
    insert into test values (3,'章子怡')
    insert into test values (4,'刘若英')
    insert into test values (8,'王菲')
    
    select * from test
    
    
    
    /**************** 实现更改自动增长列********************/
    
    begin transaction
    
    create table test_tmp
    (
    id int not null identity(1,1),
    name varchar(20) not null
    )
    go
    
    set identity_insert test_tmp on
    go
    
    if exists(select * from test)
    exec(' insert into test_tmp(id, name ) select id, name from test with(holdlock tablockx)')
    go
    
    set identity_insert test_tmp off
    go
    
    drop table test
    go
    
    exec sp_rename N'test_tmp' ,N'test' , 'OBJECT'
    go
    
    commit
    
    GO
    
    /****************验证结果*****************/
    insert into test values ('张曼')
    select * from test

    实例:

    /**************** 实现更改自动增长列********************/
    
    begin transaction
    
    create table test_tmp
    (
    UserGradeID int not null identity(1,1),
    UserGrade nvarchar(8) not null,
    [Status] int not null,
    Remark nvarchar(128) ,
    adduser nvarchar(32) ,
    upduser nvarchar(32) ,
    addtime datetime2(7) ,
    updtime datetime2(7)  
    )
    go
    
    set identity_insert test_tmp on
    go
    
    if exists(select * from m_usergrade)
    
    
    exec(' insert into test_tmp(UserGradeID,UserGrade,[Status],Remark,adduser,upduser,addtime,updtime ) 
    select UserGradeID,UserGrade,[Status],Remark,adduser,upduser,addtime,updtime from m_usergrade with(holdlock tablockx)')
    go
    
    set identity_insert test_tmp off
    go
    
    drop table m_usergrade
    go
    
    exec sp_rename N'test_tmp' ,N'm_usergrade' , 'OBJECT'
    go
    
    commit
    
    GO
  • 相关阅读:
    .Net基础:CLR基本原理
    行业软件开发商怎样来抢 BI 这块蛋糕?
    免费报表工具知多少?
    哪款报表工具更适合行业软件开发商?
    报表如何通过参数控制数据权限
    实现报表滚动到底部翻页效果
    报表 BI 选型的那些事
    零编码制作报表可能吗?
    为什么说当前报表开发的工作量主要在数据源环节?又如何解决呢?
    用存储过程和 JAVA 写报表数据源有什么弊端?
  • 原文地址:https://www.cnblogs.com/lhlong/p/6825786.html
Copyright © 2011-2022 走看看