,SQL插入记录时,怎么使其中一列值与自动增长的例值相同
请教高手,SQL插入记录时,如何使其中一列值与自动增长的例值相同啊
如题,
如例
AutoNum Same
1 1
2 2
3 3
4 4
5 5
6 6
autoNum 是自动增长字段
same字段,不能为空 每次插入记录时 same 字段值总是与AutoNum相同,看到别人表是这样,但我怎么也插不进去记录,因为same 字段不能为空 又不知道怎么取autonum的值,请高手指点。。
------解决方案--------------------
计算列。
sname as autonum
------解决方案--------------------
如题,
如例
AutoNum Same
1 1
2 2
3 3
4 4
5 5
6 6
autoNum 是自动增长字段
same字段,不能为空 每次插入记录时 same 字段值总是与AutoNum相同,看到别人表是这样,但我怎么也插不进去记录,因为same 字段不能为空 又不知道怎么取autonum的值,请高手指点。。
------解决方案--------------------
计算列。
sname as autonum
------解决方案--------------------
- SQL code
create table #tb( AutoNum int identity, Same int ) insert #tb (Same)select 1 declare @a int set @a=1 while @a<=10 begin insert #tb select @@identity+1 set @a=@a+1 end select * from #tb /* AutoNum Same 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 */
------解决方案--------------------
- SQL code
create table #ttt( AutoNum int identity, Same as AutoNum, value char(1) ) insert #ttt(value) select 'a' union all select 'b' union all select 'c' union all select 'd' union all select 'e' union all select 'f' select * from #ttt /* AutoNum Same value 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 f */ --如果还有别的字段
------解决方案--------------------
insert info(name,sex)
select '张三','男' union all
select '李四',‘女’
------解决方案--------------------
------解决方案--------------------
那你插入过后再更新一下表
update tbl set same=id
------解决方案--------------------
alter table #ttt
drop column same
go
alter table #ttt
add same as AutoNum
go
改一下表结构吧
------解决方案--------------------
- SQL code
Create Trigger UpdateSname On info After Update As Update info Set same=id FROM info a,inserted b WHERE a.AutoNum = b.AutoNum