当要往有设置自增标识字段的表插入数据,并希望同时设置好自增字段的值时,可以在insert into 的SQL语句前后分别加上一句sql语句,SET IDENTITY_INSERT TableName ON和SET IDENTITY_INSERT TableName OFF
//tb_Users 的id字段是种子为1的自增标识字段 SET IDENTITY_INSERT tb_Users ON if not exists(select * from tb_Users where id = 1) begin insert into tb_Users (id,name,url) values(1,'曹永思','http://www.cnblogs.com/yonsy') end else begin update tb_Users set id = 1, name = '曹永思', url = 'http://www.cnblogs.com/yonsy' where id = 1 end SET IDENTITY_INSERT tb_Users OFF