插入时正在生成标识插入错误(不在代码中)



我正试图用以下代码构建一个表-创建表时没有错误,我可以看到结构,它显示IDENTITY_INSERT设置为ON

CREATE TABLE lt_percent_cs
(
id_key INT IDENTITY PRIMARY KEY,   
customer_no INT ,
season INT,
percentage DECIMAL,
created_by VARCHAR(15) NULL,
create_dt DATETIME NULL,
last_updated_by VARCHAR(15) NULL,
last_update_dt DATETIME NULL,
create_loc VARCHAR(16) NULL
) ON [primary]
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].lt_percent_cs 
ADD CONSTRAINT [lt_percent_created_by] 
DEFAULT (user_name()) FOR [created_by]
GO
ALTER TABLE [dbo].lt_percent_cs 
ADD CONSTRAINT [lt_percent_create_dt]  
DEFAULT (getdate()) FOR [create_dt]
GO
ALTER TABLE [dbo].lt_percent_cs 
ADD CONSTRAINT [lt_percent_create_loc]  
DEFAULT [dbo].fs_location() FOR [create_loc]
GO
SET IDENTITY_INSERT lt_percent_cs ON

当我尝试插入数据时(通过应用程序而不是通过代码),会出现以下错误。

上次错误:数据库更新失败:
dataobject=

sqlca.sqlerrtext=SQLSTATE=42000
Microsoft OLE DB Provider for SQL Server
当identity_insert设置为OFF时,无法在表'lt_percent_cs'中插入标识列的显式值。

未对数据库进行任何更改。

插入lt_percent_cs(id_key,customer_no,季节,百分比)
值(54891800555142017,50)

sqlca.sqldbcode=544

sql语法:
插入lt_percent_cs(id_key,customer_no,season,percent)值(5489100555142017,50)

行:1[nvo_ds_data.uf_update.34(544)]

当我在SQL Server Management Studio中运行脚本时,我应该添加一点,它可以正常工作,不会产生任何错误。

INSERT INTO lt_percent_cs (id_key, customer_no, season, percentage) 
VALUES (54891, 80055514, 2017, 50)

有什么想法吗?

从Insert语句中删除id_key,因为id_key是身份种子

INSERT INTO lt_percent_cs (customer_no, season, percentage ) VALUES ( 80055514, 2017, 50 )

否则声明id_key as integer Primary Key那么Insert应该可以

INSERT INTO lt_percent_cs ( id_key, customer_no, season, percentage ) VALUES ( 54891, 80055514, 2017, 50 )

由于您需要大容量插入数据并维护身份,我可以建议将id_key更改为integer主键,然后大容量插入,然后将字段改回身份为ON……接下来,您不需要插入id_key

如果您想在应用程序中插入显式Id,您必须设置Identity_insert并插入一个事务!我不知道你在使用哪个应用程序,但这个规则是通用的。

最新更新