如果您可以访问DAC(专用管理员控制台),则可以INT
通过查看中的idtval
列来检查标识列的值sys.syscolpars
。
感谢Martin Smith通过Roi Gavish提出的有关此问题的非常有用的答案将我引导到该表。
以下面的临时表为例:
USE tempdb;
CREATE TABLE #d
(
ID INT NOT NULL IDENTITY(1,1)
);
TRUNCATE TABLE #d;
DBCC CHECKIDENT ('#d',RESEED, 2147483635);
INSERT INTO #d DEFAULT VALUES;
让我们看看表包含什么:
SELECT *
FROM #d;
+------------+
| ID |
+------------+
| 2147483635 |
+------------+
身份值可以通过以下代码检查:
DECLARE @idtval VARBINARY(64);
SELECT @idtval = scp.idtval
FROM sys.syscolpars scp
INNER JOIN sys.objects o ON scp.id = o.object_id
WHERE o.name LIKE '#d____%'
DECLARE @LittleEndian NVARCHAR(10);
SET @LittleEndian = LEFT(sys.fn_varbintohexstr(@idtval), 10);
SELECT @LittleEndian;
DECLARE @BigEndian NVARCHAR(10) = '0x';
DECLARE @Loop INT = 0;
WHILE @Loop < 4
BEGIN
SET @BigEndian = @BigEndian + SUBSTRING(@LittleEndian, ((4 - @Loop) * 2) + 1, 2);
SET @Loop += 1;
END
SELECT CurrentIdentityValue = CONVERT(INT,
CONVERT(VARBINARY(32), @BigEndian, 1), 2);
+----------------------+
| CurrentIdentityValue |
+----------------------+
| |
| 2147483635 |
+----------------------+
对于BIGINT
标识列,我们需要扩展代码中使用的一些变量的大小,例如:
CREATE TABLE #dBig
(
ID BIGINT NOT NULL IDENTITY(1,1)
);
TRUNCATE TABLE #dBig;
DBCC CHECKIDENT ('#dBig',RESEED, 9223372036854775704);
INSERT INTO #dBig DEFAULT VALUES;
SELECT *
FROM #dBig;
DECLARE @idtval VARBINARY(64);
SELECT @idtval = scp.idtval
FROM sys.syscolpars scp
INNER JOIN sys.objects o ON scp.id = o.object_id
WHERE o.name LIKE '#dBig____%'
DECLARE @LittleEndian NVARCHAR(18);
SET @LittleEndian = LEFT(sys.fn_varbintohexstr(@idtval), 18);
DECLARE @BigEndian NVARCHAR(18) = '0x';
DECLARE @Loop INT = 0;
WHILE @Loop < 8
BEGIN
SET @BigEndian = @BigEndian + SUBSTRING(@LittleEndian, ((8 - @Loop) * 2) + 1, 2);
SET @Loop += 1;
END
SELECT CurrentIdentityValue = CONVERT(BIGINT,
CONVERT(VARBINARY(32), @BigEndian, 1), 2);
结果BIGINT
:
+----------------------+
| CurrentIdentityValue |
+----------------------+
| |
| 9223372036854775704 |
+----------------------+