曹永思

导航

如何往有自增标识字段的表插入数据时,同时给自增标识字段插入值呢,在Inset Into语句前后加上SQL语句:SET IDENTITY_INSERT TableName ON和SET IDENTITY_INSERT TableName OFF

当要往有设置自增标识字段的表插入数据,并希望同时设置好自增字段的值时,可以在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

 

posted on 2017-05-08 09:43  曹永思  阅读(252)  评论(0编辑  收藏  举报