当IDENTITY_INSERT设置为OFF时,SQL无法在表'Table'中为identity列插入显式值[duplicate]

时间:2022-12-10 07:53:40

This question already has an answer here:

这个问题在这里已有答案:

Structure tables and result query on sqlfiddle

sqlfiddle上的结构表和结果查询

I want use query:

我想使用查询:

INSERT INTO Price (id_price, id_firm, id_city, name) 
        VALUES 
        ('12002', '1429', '73041', 'АРЕНДА (ПРОКАТ) АВТОКРАНА Г/П 25Т'),
        ('12003', '1429', '73041', 'ПЛИТКА КЕРАМИЧЕСКАЯ ГРАНИТ (КЕРАМОГРАНИТ) АССОРТ.'),
        ('12004', '1429', '73041', 'РАБОТЫ ГРУЗОПОДЪЕМНЫЕ АВТОВЫШКА (ПОДЪЕМНИК)'),
        ('12005', '1429', '73041', 'РАБОТЫ ГРУЗОПОДЪЕМНЫЕ АВТОКРАНОМ Г/П 25Т'),
        ('12006', '1429', '73041', 'РАБОТЫ КОМПРЕССОРОМ ВСЕ ВИДЫ'),
        ('12007', '1429', '73041', 'РАБОТЫ ПОГРУЗОЧНО-РАЗГРУЗОЧНЫЕ АВТОКРАНОМ-МАНИПУЛЯТОРОМ ГРУЖУ-ВОЖУ НА БАЗЕ ГАЗ Г/П 4Т'),
        ('12008', '1429', '73041', 'РАБОТЫ СПЕЦТЕХНИКОЙ ВСЕ ВИДЫ'),
        ('12009', '1429', '73041', 'РАБОТЫ СТРОИТЕЛЬНЫЕ ВСЕ ВИДЫ'),
        ('120010', '1429', '73041', 'ФОРСУНКА Д/КАМАЗ ДВИГАТЕЛЬ КАММИНС (CUMMINS) АССОРТ.');

But i get errors:

但我得到错误:

Cannot insert explicit value for identity column in table 'Price' when IDENTITY_INSERT is set to OFF.:
    INSERT INTO Price (id_price, id_firm, id_city, name) 
    VALUES 
    ('12002', '1429', '73041', 'АРЕНДА (ПРОКАТ) АВТОКРАНА Г/П 25Т'),
    ('12003', '1429', '73041', 'ПЛИТКА КЕРАМИЧЕСКАЯ ГРАНИТ (КЕРАМОГРАНИТ) АССОРТ.'),
    ('12004', '1429', '73041', 'РАБОТЫ ГРУЗОПОДЪЕМНЫЕ АВТОВЫШКА (ПОДЪЕМНИК)'),
    ('12005', '1429', '73041', 'РАБОТЫ ГРУЗОПОДЪЕМНЫЕ АВТОКРАНОМ Г/П 25Т'),
    ('12006', '1429', '73041', 'РАБОТЫ КОМПРЕССОРОМ ВСЕ ВИДЫ'),
    ('12007', '1429', '73041', 'РАБОТЫ ПОГРУЗОЧНО-РАЗГРУЗОЧНЫЕ АВТОКРАНОМ-МАНИПУЛЯТОРОМ ГРУЖУ-ВОЖУ НА БАЗЕ ГАЗ Г/П 4Т'),
    ('12008', '1429', '73041', 'РАБОТЫ СПЕЦТЕХНИКОЙ ВСЕ ВИДЫ'),
    ('12009', '1429', '73041', 'РАБОТЫ СТРОИТЕЛЬНЫЕ ВСЕ ВИДЫ'),
    ('120010', '1429', '73041', 'ФОРСУНКА Д/КАМАЗ ДВИГАТЕЛЬ КАММИНС (CUMMINS) АССОРТ.');

Tell me please why i get errors and how correct insert data ?

请告诉我为什么我会收到错误以及插入数据的正确性如何?

2 个解决方案

#1


21  

SET IDENTITY_INSERT Table_Name ON;

/* Do your Inserts */

SET IDENTITY_INSERT Table_Name OFF;

Note

Not a good practice, not advised at all. You may very well end up having duplicate values so let the identity column generate the values for you. if you want to be able to insert the values yourself then do not make it an identity column at all.

这不是一个好习惯,根本没有建议。你可能最终会有重复的值,所以让identity列为你生成值。如果您希望能够自己插入值,请不要将其设置为标识列。

#2


1  

If this is the error than: you can do either Truncate and reload the table or

如果这是错误:您可以执行Truncate并重新加载表或

` 
  SET IDENTITY_INSERT Tablename ON/OFF
 `

#1


21  

SET IDENTITY_INSERT Table_Name ON;

/* Do your Inserts */

SET IDENTITY_INSERT Table_Name OFF;

Note

Not a good practice, not advised at all. You may very well end up having duplicate values so let the identity column generate the values for you. if you want to be able to insert the values yourself then do not make it an identity column at all.

这不是一个好习惯,根本没有建议。你可能最终会有重复的值,所以让identity列为你生成值。如果您希望能够自己插入值,请不要将其设置为标识列。

#2


1  

If this is the error than: you can do either Truncate and reload the table or

如果这是错误:您可以执行Truncate并重新加载表或

` 
  SET IDENTITY_INSERT Tablename ON/OFF
 `