简体   繁体   English

在表中查找标识列

[英]Finding identity column in a Table

When trying to run an insert statement I get the following error: 尝试运行insert语句时,我收到以下错误:

Msg 544, Level 16, State 1, Line 3 Cannot insert explicit value for identity column in table 'IV00101' when IDENTITY_INSERT is set to OFF. Msg 544, Level 16, State 1, Line 3 Cannot insert explicit value for identity column in table 'IV00101' when IDENTITY_INSERT is set to OFF. Msg 544, Level 16, State 1, Line 3 Cannot insert explicit value for identity column in table 'IV00101' when IDENTITY_INSERT is set to OFF.

Is there a simple way to find what identity column I am trying to insert into that is causing this error? 有没有一种简单的方法来查找我尝试插入的标识列导致此错误?

Problem is my insert statement has 84 values I am inserting into. 问题是我的插入语句有84个值我插入。

I am using Microsoft SQL 2008 我正在使用Microsoft SQL 2008

SELECT name 
  FROM sys.columns 
  WHERE [object_id] = OBJECT_ID('dbo.IV00101') 
  AND is_identity = 1;

Your question is a bit unclear, but I'll take a stab at it: 你的问题有点不清楚,但我会抓住它:

It sounds like the identity column is auto-incrementing, and you're trying to insert the value. 听起来像标识列是自动递增的,并且您正在尝试插入该值。

You can use this query to get the identity column for all tables: 您可以使用此查询来获取所有表的标识列:

select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME 

You can query the system objects info to find out which columns are identity 您可以查询系统对象信息以找出哪些列是标识

SELECT c.name
FROM
    sys.columns c 
    JOIN sys.objects o  ON c.object_id = o.object_id
WHERE 
    o.name = 'TABLE_NAME'   -- replace with table nae 
    AND c.is_identity = 1
select name 
from sys.identity_columns 
where [object_id] = object_id('your_table_here')

If your table has a primary key, the identity column will probably be the primary key, as already mentioned in James Johnson's answer . 如果您的表具有主键,则标识列可能是主键,正如James Johnson的回答中所述

If it's not, you could just let SQL Server Management Studio create the complete CREATE TABLE script for the table, and search for the word IDENTITY inside it. 如果不是,您可以让SQL Server Management Studio为表创建完整的CREATE TABLE脚本,并在其中搜索单词IDENTITY

In my opinion, this is the easiest and fastest way to to this (and sufficient for a one-time action). 在我看来,这是实现这一目标的最简单,最快捷的方式(足以实现一次性行动)。


EDIT: 编辑:

@Aaron Bertrand: @Aaron Bertrand:
I know that primary key and identity are two different concepts. 我知道主键和身份是两个不同的概念。
But, honestly: of all the tables with an identity column that you ever saw, how many did you see where the identity column was not the primary key? 但是,老实说:在您看过的所有带有标识列的表中,您看到有多少标识列不是主键?
You are saying yourself that this is common practice. 你是在说自己这是常见的做法。

So, what I'm saying is this: 所以,我所说的是:
Yes, finding the primary key in SSMS is IMO easier than finding the identity column. 是的,在SSMS中找到主键比找到标识列更容易。
(honestly: I don't know any better way to find the identity column using SSMS, that's why I suggested the way I described above) (老实说:我不知道有什么更好的方法可以找到使用SSMS的标识栏,这就是为什么我建议我上面描述的方式)
--> if finding the primary key also means finding the identity in 95% of all cases, why not try to use the easy/fast way (find the primary key) first? - >如果找到主键也意味着在95%的情况下找到身份,为什么不首先尝试使用简单/快速方式(找到主键)?
If the primary key is not the identity column, you can still search for the identity column using the "correct" way from your answer (which is without any doubt the correct way, but not as easy as finding the primary key in the UI). 如果主键不是标识列,您仍然可以使用答案中的“正确”方式搜索标识列(这无疑是正确的方法,但不像在UI中查找主键那么容易) 。

您正尝试覆盖默认标识,为此,您需要在查询顶部添加此标识

SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM