07-08
18

IDENTITY_Insert 设置为 OFF 时,不能向表 'ORDERS' 中的标识列插入显式值

错误提示如下:
引用内容 引用内容
IDENTITY_Insert 设置为 OFF 时,不能向表 'ORDERS' 中的标识列插入显式值。
Could not synchronize database state with session


我估计是关联关系造成。但弄了半天没弄好,最后只好
Drop TABLE orDERS
GO
好在是测试时的项目。。。
然后再建表
create table orders (
   id int not null,
   begintime datetime null,
   state tinyint null,
   bank varchar(255) null,
   cost double precision null,
   u_id int null,
   primary key (id)
)
问题解决
对照hibernate生成的SQL比较,少了两句:
alter table orders add constraint FK8D444F0536A125 foreign key (u_id) references Users
alter table order_List add constraint FKA3D5932F33E6EB foreign key (o_id) references orders
估计是上面这两句的问题


下面介绍一下:SET IDENTITY_Insert
引用内容 引用内容
允许将显式值插入表的标识列中。

语法
SET IDENTITY_Insert [ database.[ owner.] ] { table } { ON | OFF }

参数
database

是指定的表所驻留的数据库名称。

owner

是表所有者的名称。

table

是含有标识列的表名。

注释
任何时候,会话中只有一个表的 IDENTITY_Insert 属性可以设置为 ON。如果某个表已将此属性设置为 ON,并且为另一个表发出了 SET IDENTITY_Insert ON 语句,则 Microsoft® SQL Server™ 返回一个错误信息,指出 SET IDENTITY_Insert 已设置为 ON 并报告此属性已设置为 ON 的表。

如果插入值大于表的当前标识值,则 SQL Server 自动将新插入值作为当前标识值使用。

SET IDENTITY_Insert 的设置是在执行或运行时设置,而不是在分析时设置。

权限
执行权限默认授予 sysadmin 固定服务器角色和 db_owner 及 db_ddladmin 固定数据库角色以及对象所有者。

示例
下例创建一个含有标识列的表,并显示如何使用 SET IDENTITY_Insert 设置填充由 Delete 语句导致的标识值中的空隙。

-- Create products table.
Create TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))
GO
-- Inserting values into products table.
Insert INTO products (product) VALUES ('screwdriver')
Insert INTO products (product) VALUES ('hammer')
Insert INTO products (product) VALUES ('saw')
Insert INTO products (product) VALUES ('shovel')
GO

-- Create a gap in the identity values.
Delete products
Where product = 'saw'
GO

Select *
FROM products
GO

-- Attempt to insert an explicit ID value of 3;
-- should return a warning.
Insert INTO products (id, product) VALUES(3, 'garden shovel')
GO
-- SET IDENTITY_Insert to ON.
SET IDENTITY_Insert products ON
GO

-- Attempt to insert an explicit ID value of 3
Insert INTO products (id, product) VALUES(3, 'garden shovel').
GO

Select *
FROM products
GO
-- Drop products table.
Drop TABLE products
GO


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1235
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭