| 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 onINSERT 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.TypFROM Complete_products LEFT OUTER JOIN tblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLMsg 545, Level 16, State 1, Line 2Explicit 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" |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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.TypFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLMike"oh, that monkey is going to pay" |
 |
|
|
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. |
 |
|
|
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 wantDECLARE @MaxId intSELECT @MaxID=MAX(PRODUCT_ID)FROM tblProductSELECT 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 #TempFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLINSERT 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.TypFROM #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,... |
 |
|
|
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.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
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 onINSERT 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.TypFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLMsg 2601, Level 14, State 1, Line 2Cannot 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 intSELECT @MaxID=MAX(PRODUCT_ID)FROM tblProductSELECT 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 #TempFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLINSERT 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.TypFROM #TempThat gave this error;Msg 102, Level 15, State 1, Line 6Incorrect 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. |
 |
|
|
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 onINSERT 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.TypFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLMsg 2601, Level 14, State 1, Line 2Cannot 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 intSELECT @MaxID=MAX(PRODUCT_ID)FROM tblProductSELECT 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 #TempFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLINSERT 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.TypFROM #TempThat gave this error;Msg 102, Level 15, State 1, Line 6Incorrect 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. |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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, TypFROM #Temp |
 |
|
|
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 onDECLARE @MaxId intSELECT @MaxID=MAX(productID)FROM tblProductSELECT 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 #TempFROM Complete_products LEFT OUTER JOINtblProduct AS tblProduct_1 ON Complete_products.APNum = tblProduct_1.productCodewhere tblProduct_1.productCode IS NULLINSERT 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, TypFROM #Temp(236752 row(s) affected)Msg 2601, Level 14, State 1, Line 13Cannot insert duplicate key row in object 'dbo.tblProduct' with unique index 'IX_tblProductt_productCode'.The statement has been terminated. |
 |
|
|
|
|
|