通常情况下,不能向 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。

或者写插入语句时不要把自增的ID加入进去。

报错:Cannot insert explicit value for identity column in table 't' when identity_insert is set to OFF的更多相关文章

  1. JPA 报错解决方案 com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'test_db' when IDENTITY_INSERT is set to OFF.

    这种错误插入数据时就是hibernate的自增长字段生成规则应该用native 在字段前加入注解 @GeneratedValue(generator="generator") @G ...

  2. 【hibernate postgresql】注解@TypeDef/@Enumerated/数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" is of type gender but expression is of type character varying

    数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" ...

  3. An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON

    If you run into the following error message: An explicit value for the identity column in table '< ...

  4. mybaties报错:There is no getter for property named 'table' in 'class java.lang.String'

    报错是由于xml里获取不到这个table参数 package com.xxx.mapper; import java.util.List; import org.apache.ibatis.annot ...

  5. 数据库报错:OracleDialect does not support identity key generation

    当我把数据库改为oracle时,项目报错:org.hibernate.MappingException: org.hibernate.dialect.OracleDialect does not su ...

  6. OGG应用进程abend报错无法insert虚拟列

    环境11.2.0.4 linux6.9 RAC2节点,ogg版本Version 12.2.0.1.160823 OGGCORE_OGGADP.12.2.0.1.0_PLATFORMS_161019.1 ...

  7. 报错:pymysql.err.InternalError: (1054, "Unknown column 'AType' in 'field list'")

    报错背景: 报错前sql代码展示: List = ['] # sql = "insert into test(id, name) value ("+"'"+ L ...

  8. 数据泵导入,报错:ORA-12899: value too large for column "SCOTT"."TEST112"."JOIN" (actual: 9, maximum: 8)

    1.报错: 数据泵执行导入时报错:ORA-12899: value too large for column "SCOTT"."TEST112"."J ...

  9. Mybatis 未设置主键映射报错;Cause: java.sql.SQLSyntaxErrorException: Unknown column 'system_id' in 'field list'

    使用MyBatis的时候,主键的字段建议绑定在Bean的属性上面, import javax.persistence.*; public class User { @Id @Column(name = ...

随机推荐

  1. svg学习笔记(二)

    SMIL animation演示代码集锦 <svg width="1400" height="1600" xmlns="http://www.w ...

  2. hbuider 中点击就显示出一个单选的列表 ,然后后台跨域向里面动态添加数据,注意里面的格式是json object

    jsp页面: <li class="mui-table-view-cell" onclick="showActionSheet()"> <di ...

  3. linux下 链接 sqlserver数据库 驱动的安装

    1.必需安装freetds 安装pdo_dblib扩展首先需要安装freetds. freeTDS的最新稳定版是0.91,这个可以在官网上下载http://www.freetds.org/ ,也可以在 ...

  4. pyqt中使用matplotlib绘制动态曲线

    一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...

  5. jsp查询页面和结果页面在同一页面显示和交互

    用frameset实现查询页面和结果页面在同一页面 用target实现交互显示在同一页面上 请参照以下方法解决: main.jsp: <html> <head> <met ...

  6. raywenderlich-iOS设计模式Part 1/2【译】

    原文地址:http://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1 Update 04/22 ...

  7. 关于MATLAB中的tic toc的问题

    关于MATLAB中的tic toc的问题 其一) MATLAB实际单位时间计时函数的具体应用,在编写程序时,经常需要获知代码的执行实际时间,这就需要在程序中用到计时函数,matlab中提供了以下三种方 ...

  8. Jquery UI dialog 传参

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3519318.html] $("#dialog").dialog({ aut ...

  9. C++的try catch到底能防止什么错误?

    我在.h文件里定义:    LoadingWidget* w;然后.cpp文件里定义: void MyClass::ModifyTask(){    // w = new LoadingWidget( ...

  10. 力控ADO组件数据源设置

    1.mysql的ODBC驱动如何下载及安装 地址:http://dev.mysql.com/downloads/connector/odbc/5.1.html Mysql跟力控ado进行交互 第一步: ...