I have below query. Logically, the procedure usp_mytran
should RESEED the Identity to 1 for table dbo.Sales
. But the last query is returning different values for Max_ID_Value and Current_Seed_Value. Can anyone please explain why DBCC command is not working inside procedure?
USE tempdb
-- Create table
CREATE TABLE dbo.Sales
(ID INT IDENTITY(1,1), Address VARCHAR(200))
GO
-- Procedure to Populate data into dbo.Sales
CREATE PROCEDURE usp_mytran
AS
BEGIN
BEGIN TRANSACTION
INSERT dbo.Sales
( Address )
VALUES ( 'Dwarka, Delhi' );
-- Delete it for some reason
DELETE FROM dbo.Sales;
-- Code to check max ID value, and verify it again IDENTITY SEED
DECLARE @MaxValue INT = (SELECT ISNULL(MAX(ID),1) FROM dbo.Sales)
IF @MaxValue IS NOT NULL AND @MaxValue <> IDENT_CURRENT('dbo.Sales')
DBCC CHECKIDENT ( 'tempdb.dbo.Sales', RESEED, @MaxValue );
ROLLBACK TRANSACTION
END
-- Ideally, this should RESEED the Identity of dbo.Sales table.
EXEC usp_mytran
-- Max_ID_Value & Current_Seed_Value should be same
SELECT ISNULL(MAX(ID),1) AS Max_ID_Value, IDENT_CURRENT('dbo.Sales') AS Current_Seed_Value FROM dbo.Sales