Identity Insert Problem
I am trying to run this script:
SET IDENTITY_INSERT dbo.Message ON
INSERT INTO dbo.Message
SELECT (Values I want to insert)
and when I do I still get the error *An explicit value for the identity column in table 'dbo开发者_JAVA技巧.Message' can only be specified when a column list is used and IDENTITY_INSERT is ON.*
What am I doing wrong?
The key to your error is "when a column list is used". You want:
SET IDENTITY_INSERT dbo.Message ON
INSERT INTO dbo.Message (column1, column2, ...) -- Added column list here
SELECT (Values I want to insert)
You need to do just as the error message says. Format your code like:
INSERT INTO dbo.Message
(col1, col2, col3, col4)
SELECT Col1, col2, col3, col4
FROM OtherTable
You need the list of fields after the INSERT
line, and you need to specify the field names in your SELECT
- SELECT *
won't work.
You have to mention all the columns Name in both Insert Statement and in select clause. Some thing like this.
Insert Into tbl ( [col1], [col2] ) SELECT [col1], [col2]
精彩评论