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
 General SQL Server Forums
 New to SQL Server Programming
 Using IDENTITY_INSERT with 3 key columns

Author  Topic 

sogorman
Starting Member

5 Posts

Posted - 2010-12-21 : 08:34:23
I have a table with about 13 columns. 3 of them are key columns with auto incrementing values. I am trying to import data from one table into that table but only want to specify data for 12 of the columns (2 of which are key columns) and have SQL just auto increment the one remaining key column. The only way I have found to be able to insert data in the table and specify data for some of the key columns is using Insert Identity = on but when doing that I have to supply data for all the columns. Here is my syntax which works if supplying data for all 3 of the key columns (OffsetTrack, Offset, AutoInc) in the PlayTableOffset table. I would like to leave the AutoInc column alone and just have SQL bump that up by one for every new row.

Sean



CREATE TABLE PlayTableOffsetHolding
("OffsetTrack" char(15),
"Offset" char(12),
"AutoInc" int,
"Action" char(255),
"Device" char(255),
"Option1" char(255),
"Option2" char(255),
"Option3" char(255),
"Option4" char(255),
"Option5" char(255),
"Option6" char(255),
"Option7" char(255),
"Option8" char(255),
"Option9" char(255),
"Option10" char(255)
)


INSERT INTO PlayTableOffsetHolding
(OffsetTrack)
SELECT PlayTableHolding.OffsetTrack
FROM PlayTableHolding

Update PlayTableOffsetHolding
SET Offset='+00:00:20:00', "AutoInc" = '12345', "Action" = 'RunMacro', "Device" = 'Macro', "Option1" = 'Cox7InHD'


SET IDENTITY_INSERT PlayTableOffset ON

INSERT INTO PlayTableOffset
("OffsetTrack",
"Offset",
"AutoInc",
"Action",
"Device",
"Option1",
"Option2",
"Option3",
"Option4",
"Option5",
"Option6",
"Option7",
"Option8",
"Option9",
"Option10")

SELECT "OffsetTrack",
"Offset",
"AutoInc",
"Action",
"Device",
"Option1",
"Option2",
"Option3",
"Option4",
"Option5",
"Option6",
"Option7",
"Option8",
"Option9",
"Option10"
FROM PlayTableOffsetHolding

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-12-21 : 11:37:03
How did you get SQL to auto increment three columns? Especially when two of them are CHAR columns. I thought you could only create one IDENTITY column and one ROWGUIDCOL column per table.
Go to Top of Page
   

- Advertisement -