We all had to start somewhere - for me it was back when Dinosaurs roamed the earth!!
CREATE TABLE Tbl_MyTest_InsertTable
(
[LocationID] [int] Not Null,
[DataSourceID] [int],
[MergeID] [int] IDENTITY(6000,1) Not Null,
)
Go
SET IDENTITY_INSERT Tbl_MyTest_InsertTable ON
INSERT INTO Tbl_MyTest_InsertTable (LocationID, DataSourceID, MergeID)
Values ('6', '6', '6000')
My thought was that you will "pull" the data from your existing table at this point e.g.
INSERT INTO Tbl_MyTest_InsertTable
(
LocationID, DataSourceID, MergeID
)
SELECT LocationID, DataSourceID,
LocationID + 6000 -- Have I understood correctly this is the value you want for MergeID?
From tbl_Location_DataSource
Where DataSourceID = '62'
If you want something different to LocationID + 6000 then please explain what you need. For example, if LocationID has gaps, or doesn't start at "1", and you just want sequence, contiguous, numbers 6000, 6001, ... then SQL can give you a "Row_Number" which you could use instead
SET IDENTITY_INSERT Tbl_MyTest_InsertTable OFF
DECLARE @MaxMergeID int
SELECT @MaxMergeID = MAX( MergeID)+1 FROM Tbl_MyTest_InsertTable
DBCC CHECKIDENT (Tbl_MyTest_InsertTable, RESEED, @MaxMergeID)
next you can insert all the rows that are NOT DataSourceID = '62' e.g.
INSERT INTO Tbl_MyTest_InsertTable
(
LocationID, DataSourceID --, MergeID
)
SELECT LocationID, DataSourceID
From tbl_Location_DataSource
Where DataSourceID <> '62'
OR DataSourceID IS NULL
and these will be inserted "after" the DataSourceID = '62' records - i.e. with "higher" MergeID values)
Just in case what you want to do is:
Do you want to import DataSourceID = '62' with a MergeID starting at 6000 but WITHOUT it needing to be equivalent to the LocationID?; and then import the DataSourceID <> '62' with higher MergeID number (but, again, without any association of MergeID value with LocationID value?)
If so then you could do that without having to mess about with setting IDENTITY_INSERT to ON / OFF. Create your new target-table as before:
CREATE TABLE Tbl_MyTest_InsertTable
(
[LocationID] [int] Not Null,
[DataSourceID] [int],
[MergeID] [int] IDENTITY(6000,1) Not Null,
)
Go
and then insert your data in one batch with the DataSourceID = '62' rows sorted first, so they will get MergeID 6000, 6001, etc. and then sort the remainder (i.e. DataSourceID <> '62' ) rows second. Within those two groups you could sub-sort by, say, LocationID so that the MergeID numbers are allocaed in ascending LocationID order (or any other column(s) that you choose as a Tie Break in the ORDER BY).
INSERT INTO Tbl_MyTest_InsertTable
(
LocationID, DataSourceID
)
SELECT LocationID, DataSourceID
From tbl_Location_DataSource
ORDER BY
CASE WHEN DataSourceID = '62'
THEN 1 --"First"
ELSE 2 --"Subsequent"
END,
LocationID -- List any "tie break" sequencing columns, in precedence order