SQL Server自增字段插入值的实际操作

数据库 SQL Server
此文章主要向大家描述的是向SQL Server自增字段插入值的实际操作步骤,以下就是对其实际操作的详细解说,望大家有所收获。

在实际操作中我们经常会遇到向SQL Server自增字段插入值的这一状况,以下的文章主要向大家描述的是向SQL Server自增字段插入值的实际操作步骤,在一般的情况下,不能直接向 SQL Server 数据库的自增字段插入值。

如果非要这么干的话,SQL Server 就会好不客气地给你个错误警告:

  1. Server: Msg 544, Level 16, State 1, Line 1  
  2. Cannot insert explicit value for identity column in table 't' when identity_insert is set to OFF. 

这个错误消息提示我们,如果向 SQL Server 自增字段插入值,需要设置 identity_insert 选项为 on。

  1. set identity_insert on 

看具体的一个例子:

  1. create table dbo.t  
  2. (  
  3. id int identity(1,1) not null,  
  4. name varchar(50)  
  5. )  
  6. set identity_insert t on  
  7. insert into t (id, name) values(1, 'sqlstudy')  
  8. set identity_insert t off  

注意的是,自增字段插入值后,要及时把 identity_insert 设置为 off。

上述的相关内容就是对SQL Server自增字段插入值的描述,希望会给你带来一些帮助在此方面。

向SQL Server自增字段插入值

以上的相关内容就是对SQL Server自增字段插入值的介绍,望你能有所收获。

【编辑推荐】

  1. 对SQL Server SQL语句进行优化的10个原则
  2. C#来对SQL Server存储过程进行创建
  3. SQL Server导入升级还有什么你没做?
  4. SQL Server索引的使用误区讲述
  5. SQL Server索引实际结构的理解

 

责任编辑:佚名 来源: 互联网
相关推荐

2010-06-28 09:59:47

SQL Server自

2010-10-20 10:04:36

sql server自

2010-06-18 13:47:21

SQL Server数

2010-09-28 16:14:14

SQL Server自

2010-07-21 15:22:07

2010-07-07 11:03:21

SQL Server索

2010-04-16 13:59:40

Oracle数据

2010-07-16 11:10:52

SQL server

2010-06-28 12:39:14

SQL Server数

2010-07-23 09:25:50

SQL Server导

2010-04-09 09:28:30

Oracle自增字段

2010-06-30 17:56:06

2010-06-28 12:27:35

SQL Server

2010-06-28 13:27:33

SQL Server视

2010-06-17 12:26:51

SQL Server索

2010-07-12 10:13:44

SQL Server表

2010-07-05 12:21:57

SQL Server记

2010-07-06 09:20:30

SQL Server查

2010-06-18 08:30:48

SQL Server

2010-07-02 11:10:56

SQL Server
点赞
收藏

51CTO技术栈公众号