I'm grabbing some rows from a table, manipulating them in a temp table, and then looking to insert them as new rows into my original table.
However, I'm running into an issue with the identity column, even when I don't have the identity column on my temp table. The identity column is an auto-incrementing int.
This seems like a simple thing I'm way overthinking.
select top 0 *
into #TestTable
from OriginalTable;
...
--insert and manipulate records
...
ALTER TABLE #TestTable
DROP COLUMN MyIdentityColumn;
DECLARE #InsertedRows TABLE (NewSeqNum INT);
INSERT INTO OriginalTable
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT * FROM #TestTable
but I get this error:
An explicit value for the identity column in table 'OriginalTable' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I absolutely do not want to set an explicit value, I want it to insert and give me the new identity (via #InsertedRows)
If you don't want to keep the id of inserted records, then you need to specify all your columns but the id column in the select. As general good practice, dont select *, always specify the columns you want to retrieve-insert.
INSERT INTO OriginalTable (col1, col2, col3...)
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT (col1, col2, col3...) FROM #TestTable
If I'm understanding you, I think your problem is that you're trying to insert '*' into the original table - which means all of your columns from the temp table. Including your ID column (which you don't want to insert, because you're wanting it to auto-generate.)
Instead, I'd suggest doing something like this:
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your temp table
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your original table.
... aka, spell out the columns explicitly, and omit the Identity column.
I have a table which has several field including:
contact_id
phone
phone_id
contact_id and phone are primary keys and phone_id is an auto increment field. I want to use it to recognize a certain entry. So I want to know that is it possible to duplicate that non primary field when I'm entering data.
Unless there is no constraint, some unique index, you can duplicate values in that column, because 1) you can turn identity_insert on, 2) you can reseed increments.
Here is a proof:
CREATE TABLE #test(id INT IDENTITY(1, 1))
INSERT INTO #test DEFAULT VALUES
INSERT INTO #test DEFAULT VALUES
INSERT INTO #test DEFAULT VALUES
SET IDENTITY_INSERT #test ON
INSERT INTO #test(id) VALUES(1)
SET IDENTITY_INSERT #test OFF
INSERT INTO #test DEFAULT VALUES
INSERT INTO #test DEFAULT VALUES
DBCC CHECKIDENT ('#test', RESEED, 1);
INSERT INTO #test DEFAULT VALUES
INSERT INTO #test DEFAULT VALUES
SELECT * FROM #test
DROP TABLE #test
Output:
id
1
2
3
1
4
5
2
3
The short answer is Yes, it's possible.
SQL Server does not force a unique constraint on identity columns, meaning that the can have duplicated values, however, Sql server will not generate duplicate values itself in an identity column.
Identity columns in sql server are populated by the sql server itself when you insert a row to the table.
However, you can specify a value to them by using SET IDENTITY_INSERT before the insert statement.
There are a couple of things that you should be aware of:
Setting identity_insert on is per table. you can only set it for one table at the time.
Until you set the identity_insert back off, any insert statement to that table will have to specify a value for the identity column.
you can't use set identity insert on for more then one table on a single session. therefor after you've done inserting records to the table you must set the identity_insert back off on that table.
I have an SSMS table that has one column that is a pk and set to auto increment. In my code I need to create a row and then pull the value of the ID.
I don't want to set the id, it needs to auto generate. Is there a way to do this without adding a second column to add data into?
Just use the phrase "DEFAULT VALUES" in place of a field list and values specification:
INSERT INTO [TableName] DEFAULT VALUES;
Test with:
CREATE TABLE #Test
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
);
INSERT INTO #Test DEFAULT VALUES;
SELECT SCOPE_IDENTITY();
For more info on this, check out the MSDN page for INSERT and do a "find" (i.e. Control-F) in your browser for "default values".
I am trying to insert from a table I created into a production table - I was able to do this earlier with a formula that I think caused duplicates and did not populate the primary key(In this case PROSPECT_ID properly. Since it is an auto incremented field and I am using a select from another table to INSERT any help on how to properly auto increment this field in production? MKT_PROSPECT
INSERT INTO MKT_PROSPECT(PROSPECT_ID,RECORD_TYPE,search_name,LABEL_NAME,FIRST_NAME,LAST_NAME,CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY_NAME,JOB_TITLE,FORMATTED_DETAIL,ADDRESS_1,ADDRESS_2,CITY,STATE,POSTAL_CODE,ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,HOME_PHONE,EMAIL,ADDOPER)
select PROSPECT_ID,RECORD_TYPE,SEARCH_NAME,LABEL_NAME,FIRST,LAST,CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY,TITLE,FORMATTED_DETAIL,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,PHONE,EMAIL,ADDOPER from dbo.prospects
where SEARCH_NAME IS not NULL and LABEL_NAME is not null
If you would like to insert into a table with a specific ID use IDENTITY INSERT ON as John Hartsock said in the comments. It is worth noting that the auto increment column will continue all other inserts (outside of the scope of the Identity Insert) above the largest number.
If you meant to insert and have the table auto generate you just don't include the ID column in the insert:
INSERT INTO MKT_PROSPECT(RECORD_TYPE,search_name,
LABEL_NAME,FIRST_NAME,LAST_NAME,CUSTOMER_CLASS_CODE,
CUSTOMER_STATUS_CODE,COMPANY_NAME,JOB_TITLE,
FORMATTED_DETAIL,ADDRESS_1,
ADDRESS_2,CITY,STATE,POSTAL_CODE,
ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,
HOME_PHONE,EMAIL,ADDOPER)
select RECORD_TYPE,SEARCH_NAME,LABEL_NAME,FIRST,LAST,
CUSTOMER_CLASS_CODE,CUSTOMER_STATUS_CODE,COMPANY,TITLE,
FORMATTED_DETAIL,ADDRESS1,ADDRESS2,CITY,STATE,ZIP,
ADDRESS_TYPE_CODE,ADDRESS_STATUS_CODE,
PHONE,EMAIL,ADDOPER from dbo.prospects
where SEARCH_NAME IS not NULL and LABEL_NAME is not null
don't know why this code is not executing...
SET IDENTITY_INSERT COM_MST ON
GO
INSERT INTO COM_MST
SELECT * FROM COM_MST_DEL
and showing an error
An explicit value for the identity column in table 'COM_MST' can only be specified when a column list is used and IDENTITY_INSERT is ON.
You have to specify the columns in the insert statement
SET IDENTITY_INSERT COM_MST ON
GO
INSERT INTO COM_MST
SELECT * FROM COM_MST_DEL
In this your select statement SELECT * FROM COM_MST_DEL might be returning more than the column available in table COM_MST also make sure to specify the column name in Insert statement.
OR
See the solution proposed in the post An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON SQL Server