当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值

标签:
identityinsertdatabasesqlserverit |
分类: db_general |
{"当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'OrderList' 中的标识列插入显式值"}
对于这个异常可以从两个角度来处理:A:数据库执行语句
A数据库执行语句:
问题描述:当在数据库表主键设计为 (Orderid
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />insert into OrderList(id,OrderName) values( 4520,'电子传票订单')
执行上面语句会提示一个错误:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />服务器: 消息 544,级别 16,状态 1,行 1
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" />当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'OrderList' 中的标识列插入显式值。
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
其中关于主键一条记录,当我们想把这条记录的id设置成我们自定义的4520时出现上面的错误,如果我们添加一些设置,修改方法如下:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />--允许将显式值插入表的标识列中 ON -允许 OFF -不允许
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> set identity_insert OrderList ON --打开
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" />insert into OrderList(id,ordername,createdate)
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" />values( 4520,'set',getdate())
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> set identity_insert OrderList OFF --关闭
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
在执行这个插入语句时多了一个设置,该设置的语法是
--设置语法:
SETIDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OFF }
允许将显式值插入表的标识列中
参数说明:
database:针对数据库
table:针对某张表
ON:允许插入显式值插入标识列
OFF:不允许
SET
允许将显式值插入表的标识列中
参数说明:
database:针对数据库
table:针对某张表
ON:允许插入显式值插入
OFF:不允许
注意:
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" TITLE="当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />Code
--问题注意
(1)任何时候,会话中只有一个表的IDENTITY_INSERT 属性可以设置为 ON。如果某个表已将此属性设置为 ON,并且为另一个表发出了 SET IDENTITY_INSERT ON 语句,则 Microsoft® SQL Server™ 返回一个错误信息,指出 SET IDENTITY_INSERT 已设置为 ON 并报告此属性已设置为 ON 的表
( 2)如果插入值大于表的当前标识值,则 SQL Server 自动将新插入值作为当前标识值使用
( 3)SET IDENTITY_INSERT 的设置是在执行或运行时设置,而不是在分析时设置
--问题注意
(1)任何时候,会话中只有一个表的
(
(
上面执行语句中:把要执行的语句前后加上该设置,当然上面针对事一条记录插入操作,在这条记录插入操作后,如果再次插入数据时,没有启用该设置,Orderid主键列会根据上面自定义表示4520,自动增长到4521.这个操作很灵活关键看个人怎么利用.
B:修改配置文件
我们通过直接修改配置文件来处理这个问题,
出错时文件配置:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />--hibernate持久化类配置 注意id中Generator子节点设置Class属性为:assigned自动增长
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <class name ="TestHibernateExpre.Entities.OrderListModel,TestHibernateExpre" table ="OrderList">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <id name ="Orderid" column ="id" type ="int">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <!--id中参数的设置问:native/assigned/foreign/increment-->
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <generator class="assigned"></generator>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> </id>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> </class>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
修改后文件配置:
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值" />--Hibernate中关于持久化类的配置 注意id下Generator子节点 class属性配置为native
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <class name ="TestHibernateExpre.Entities.OrderListModel,TestHibernateExpre" table ="OrderList">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <id name ="Orderid" column ="id" type ="int">
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <!--id中参数的设置问:native/assigned/foreign/increment-->
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> <generator class="native"></generator>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> </id>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT设置为 OFF 时,不能为表中的标识列插入显式值" /> </class>
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT
http://www.cnblogs.com/Images/OutliningIndicators/None.gifIDENTITY_INSERT