Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to set start identity column during an insert

Author  Topic 

kory27
Starting Member

35 Posts

Posted - 2008-05-02 : 10:46:38
Hi.

I am doing a left join insert from two tables, and i know my ID is the Indentity column. I am doing something wrong and it has to do with the identity issue. I have set the identity to on, b/c i currently have data in the table (tblProduct) and I am inserting all the records not in that field from a table named Complete_Products. Can someone please examine this query and tell me what I am doing wrong? I think I need to set a start value for the identity but not totally confident on how to do that.

Thanks. Below is my query with error.

Set Identity_Insert tblProduct on
INSERT INTO tblProduct
(productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL


Msg 545, Level 16, State 1, Line 2
Explicit value must be specified for identity column in table 'tblProduct' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.


mfemenel
Professor Frink

1421 Posts

Posted - 2008-05-02 : 10:51:38
What is the name of the column that has the identity in tblProduct?

Mike
"oh, that monkey is going to pay"
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-02 : 10:55:48
Thanks. ProductID. The last record #is 901779, which would mean it would now start at 901780 for the inserted/appended records.

thanks again.
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-05-02 : 10:56:09
specify u r identity column in the insert clause and in select stmt give the value for it
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2008-05-02 : 10:58:38
So you'd need to specify product id then in your field list and your select.
INSERT INTO tblProduct
(PRODUCT_ID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT Complete_products.PRODUCT_ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL



Mike
"oh, that monkey is going to pay"
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-02 : 11:05:50
And should that start the inserted record # at the correct place? i don't want to overwrite existing data b/c the id 1, for example, will exist in both b/c the indentity column was autogenerated on both tables.

thanks again.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-02 : 13:34:14
quote:
Originally posted by kory27

And should that start the inserted record # at the correct place? i don't want to overwrite existing data b/c the id 1, for example, will exist in both b/c the indentity column was autogenerated on both tables.

thanks again.


i think this is what you want




DECLARE @MaxId int

SELECT @MaxID=MAX(PRODUCT_ID)
FROM tblProduct

SELECT IDENTITY(1,1) AS ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ INTO #Temp
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL

INSERT INTO tblProduct
(PRODUCT_ID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT @MaxID + ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM #Temp


This will first retrieve the maximum ID value(901779) from your destination table. Then you populate a temporary table with an ID column with records you want to transfer and finally add the ID value which will be incremental 1,2,3... with max value to get PRODUCT_ID of inserted records. This ensures they will be numbered sequentially from last inserted value i.e 901780,901781,...
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-05-02 : 15:20:32
I don't understand this sentence:

quote:

I have set the identity to on, b/c i currently have data in the table (tblProduct) and I am inserting all the records not in that field from a table named Complete_Products.



That does not explain why you feel you need to set the identity values manually.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-03 : 09:29:54
Ok. So i tried the following, which gave me the following error;

Set Identity_Insert tblProduct on
INSERT INTO tblProduct
(productId,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT Complete_products.ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL


Msg 2601, Level 14, State 1, Line 2
Cannot insert duplicate key row in object 'dbo.tblProduct' with unique index 'IX_tblProductt_productCode'.
The statement has been terminated.


The way that is written, I believe it is only trying to insert records in Complete_products and not in the tblProducts. So I am thinking that it is trying to insert the ID from Complete_Products and not inserting the new record with the next available ID(that is my PK and it is Identity) That said, I then tried the later post of visakh16 and tried this query;

DECLARE @MaxId int

SELECT @MaxID=MAX(PRODUCT_ID)
FROM tblProduct

SELECT IDENTITY(1,1) AS ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ INTO #Temp
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL

INSERT INTO tblProduct
(PRODUCT_ID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT @MaxID + ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM #Temp


That gave this error;

Msg 102, Level 15, State 1, Line 6
Incorrect Syntax near '1'.

Sorry for all the questions, but each time i try to run this, the initial failure can take a while to tell me. Thanks again all.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-03 : 09:59:28
quote:
Originally posted by kory27

Ok. So i tried the following, which gave me the following error;

Set Identity_Insert tblProduct on
INSERT INTO tblProduct
(productId,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT Complete_products.ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL


Msg 2601, Level 14, State 1, Line 2
Cannot insert duplicate key row in object 'dbo.tblProduct' with unique index 'IX_tblProductt_productCode'.
The statement has been terminated.


The way that is written, I believe it is only trying to insert records in Complete_products and not in the tblProducts. So I am thinking that it is trying to insert the ID from Complete_Products and not inserting the new record with the next available ID(that is my PK and it is Identity) That said, I then tried the later post of visakh16 and tried this query;

DECLARE @MaxId int

SELECT @MaxID=MAX(PRODUCT_ID)
FROM tblProduct

SELECT IDENTITY(int,1,1) AS ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ INTO #Temp
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL

INSERT INTO tblProduct
(PRODUCT_ID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT @MaxID + ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ
FROM #Temp


That gave this error;

Msg 102, Level 15, State 1, Line 6
Incorrect Syntax near '1'.

Sorry for all the questions, but each time i try to run this, the initial failure can take a while to tell me. Thanks again all.



Oops i missed an int. Also make sure you set identity insert to on before this and turn it off after.
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-03 : 12:10:33
No it is saying all the fields that come from Complete_Products can not be bound. That is a first for me in my learning curve here.
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-03 : 12:29:09
And that was supposed to be Now, not no. Also, it didn't insert them after all.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-03 : 12:43:34
quote:
Originally posted by kory27

No it is saying all the fields that come from Complete_Products can not be bound. That is a first for me in my learning curve here.


Remove the aliases from select step from temporary table.make it like this:-

INSERT INTO tblProduct
(PRODUCT_ID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT @MaxID + ID,APNum, Title, CategoryID, Mountable, price,
Height, Width, IRank, frameable, Typ
FROM #Temp
Go to Top of Page

kory27
Starting Member

35 Posts

Posted - 2008-05-06 : 09:29:29
Ok, I ran this below with the error in red. I am confused b/c from the error it seems like it is trying to insert a duplicate product code, but shouldn't the left join be handling that so as to only insert records that the product code is in the table Complete_Products and not in tblProduct? Thanks again.

Set Identity_Insert tblProduct on
DECLARE @MaxId int

SELECT @MaxID=MAX(productID)
FROM tblProduct

SELECT IDENTITY(int,1,1) AS ID,Complete_products.APNum, Complete_products.Title, Complete_products.CategoryID, Complete_products.Mountable, Complete_products.price,
Complete_products.Height, Complete_products.Width, Complete_products.IRank, Complete_products.frameable, Complete_products.Typ INTO #Temp
FROM Complete_products LEFT OUTER JOIN
tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCode
where tblProduct_1.productCode IS NULL

INSERT INTO tblProduct
(productID,productCode, productName, productNavID, CanBeMounted, productRetailPrice, productHeight, productWidth, Rank, CanBeFramed, ProductType)
SELECT @MaxID + ID, APNum, Title, CategoryID, Mountable, price,
Height, Width, IRank, frameable, Typ
FROM #Temp


(236752 row(s) affected)
Msg 2601, Level 14, State 1, Line 13
Cannot insert duplicate key row in object 'dbo.tblProduct' with unique index 'IX_tblProductt_productCode'.
The statement has been terminated.


Go to Top of Page
   

- Advertisement -