On Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64)
, is there a way to see what mechanism SQL Server uses to calculate the new identity values for tables created by SELECT INTO
?
SAMPLE DATA
-- Create our base table
CREATE TABLE dbo.A
(A_ID INT IDENTITY(1, 1),
x1 INT,
noise1 int DEFAULT 1,
noise2 char(1) DEFAULT 'S',
noise3 date DEFAULT GETUTCDATE(),
noise4 bit DEFAULT 0);
-- Create random data between the range of [0-3]
INSERT INTO dbo.A(x1)
SELECT s1000.n FROM
( SELECT TOP (10) n = 1 FROM sys.columns) AS s10 -- 10
CROSS JOIN
( SELECT TOP (10) n = 1 FROM sys.columns) AS s100 -- 10 * 10
CROSS JOIN
( SELECT TOP (10) n = ABS(CHECKSUM(NEWID())) % 4 FROM sys.columns) AS s1000; -- 100 * 10
SELECT * FROM dbo.A ORDER BY A_ID DESC;
In my case, the results are:
And running
DBCC CHECKIDENT('A')
returns
Checking identity information: current identity value '1000', current column value '1000'.
Create new table
When we select a subset into a new table, it creates a new current identity value:
SELECT * INTO #temp FROM dbo.A WHERE x1 = 0;
The highest value in the identity column of this new table is 998 and when we check it's next identity:
DBCC CHECKIDENT('#temp')
Checking identity information: current identity value '998', current column value '998'.
How?
How are these identity values inserted into the new table with the highest value correctly set as the current identity value of the new table?