set identity_insert test on insert test select 1 set identity_insert test off
set identity_insert test on insert test(id) select 1 set identity_insert test off
SET identity_insert TB ON
先ADD COLUMN,再添加,再删除COLUMN,恩恩我果然是天才
感觉建这个表没什么价值啊?SELECT TOP 100 id = identity(int,1,1) INTO test FROM syscolumns a, syscolumns b
--参考: --操作的过程中,注意一点,标识列自增是不能修改的,那么首先 --去除该列自增的标识,然后再修改id,成功修改后,再加上标识 --r如果不修改标识,会报错:“无法更新标识列”create table #a (ids int, names varchar(100) )--插入测试数据,序号从100开始的,表示你当前表的情况 declare @a int set @a=100 while @a<200 begin insert #a(ids) select @a set @a=@a+1 end --假定还有一个#b表也引用了该序号 --插入测试数据到#b select ids n_id,names ff into #b from #a --好,现在开始处理序号问题了,用临时表#tmp过渡 --这里用了一个标识列,请注意! --将带一个新的序列的数据插入到表#tmp中 select identity(int,1,1) flag, * into #tmp from #a--看看结果是不是有了新的序列了,呵 select * from #tmp--那么现在就利用#tmp表来更新所有用到该序列的表--更新#a update #a set ids=flag from #a a,#tmp b where a.ids=b.ids--更新其他表引用了该序列的,比如#b update #b set n_id=flag from #b a,#tmp b where a.n_id=b.ids
set IDENTITY_INSERT test on insert into test(id) select @int---- 这样任意加整型 ,但不能插入null的
这样建立的表 没有任何意义的,就好像Oracle 的dual……
if object_id('test') is not null drop table testcreate table test ( id int identity(1,1) ) --怎么往表里加数据? alter table test add id1 intinsert into test select 1 go 100alter table test drop column id1 select * from test --搞定 /* id ----------- 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100(100 行受影响)*/
处理办法:若要成功地将特定的标识行插入包含标识列的表中,必须提供列的列表并且将 SET IDENTITY_INSERT 设为 ON。进行了如下操作:set IDENTITY_INSERT tb ON insert into tb(自增列)values(22)
insert test
select 1
set identity_insert test off
insert test(id)
select 1
set identity_insert test off
感觉建这个表没什么价值啊?SELECT TOP 100 id = identity(int,1,1) INTO test FROM syscolumns a, syscolumns b
--操作的过程中,注意一点,标识列自增是不能修改的,那么首先
--去除该列自增的标识,然后再修改id,成功修改后,再加上标识
--r如果不修改标识,会报错:“无法更新标识列”create table #a
(ids int,
names varchar(100)
)--插入测试数据,序号从100开始的,表示你当前表的情况
declare @a int
set @a=100
while @a<200
begin
insert #a(ids)
select @a
set @a=@a+1
end
--假定还有一个#b表也引用了该序号
--插入测试数据到#b
select ids n_id,names ff into #b from #a
--好,现在开始处理序号问题了,用临时表#tmp过渡
--这里用了一个标识列,请注意!
--将带一个新的序列的数据插入到表#tmp中
select identity(int,1,1) flag, * into #tmp from #a--看看结果是不是有了新的序列了,呵
select * from #tmp--那么现在就利用#tmp表来更新所有用到该序列的表--更新#a
update #a set ids=flag from #a a,#tmp b where a.ids=b.ids--更新其他表引用了该序列的,比如#b
update #b set n_id=flag from #b a,#tmp b where a.n_id=b.ids
insert into test(id) select @int---- 这样任意加整型 ,但不能插入null的
drop table testcreate table test
(
id int identity(1,1)
)
--怎么往表里加数据?
alter table test add id1 intinsert into test select 1
go 100alter table test drop column id1 select * from test
--搞定
/*
id
-----------
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100(100 行受影响)*/
insert into tb(自增列)values(22)