--如果你的rec字段可以从1开始从新设计的话可以 alter table tb drop column rec alter table tb add rec int identity(1,1) --也可以直接更改为自增列,不用删除!insert into a.dbo.tb(col1,col2,col3) select col1,col2,col3 from b.dbo.tb
--楼主看下面例子 use CSDN GOcreate table tb_identity ( id int identity(1, 1), col1 int, col2 int ) GOuse TEST GOcreate table tb_no_identity ( col1 int, col2 int ) GO insert tb_no_identity select 1, 2 GO--SQL INSERT CSDN.dbo.tb_identity(col2) SELECT col2 FROM TEST.dbo.tb_no_identity--test and result SELECT * FROM CSDN.dbo.tb_identity /* id col1 col2 1 NULL 2 */
--如果你的rec字段可以从1开始从新设计的话可以
alter table tb drop column rec
alter table tb add rec int identity(1,1) --也可以直接更改为自增列,不用删除!insert into a.dbo.tb(col1,col2,col3)
select col1,col2,col3 from b.dbo.tb
该列已经是自增列,但插入的时候哟是不选择这个列会提示出错,提示该列不能为空。
用insert语句也是一样不会自增 这是为什么?跨数据库的原因?如果不跨数据库,直接insert他会自增
use CSDN
GOcreate table tb_identity
(
id int identity(1, 1),
col1 int,
col2 int
)
GOuse TEST
GOcreate table tb_no_identity
(
col1 int,
col2 int
)
GO
insert tb_no_identity
select 1, 2
GO--SQL
INSERT CSDN.dbo.tb_identity(col2)
SELECT col2 FROM TEST.dbo.tb_no_identity--test and result
SELECT * FROM CSDN.dbo.tb_identity
/*
id col1 col2
1 NULL 2
*/
ID rec parrec
00001 1 0
0000100001 2 1
0000100002 3 1
0000100003 4 1
0000100004 5 1
0000100005 6 1
类似下去现在问题是:要修改parrec = 1 要怎么修改? update a set parrec = (如何填写,不能写1)
update a set parrec =rec-1