我有以下查询。逻辑上,过程usp_mytran
应该将表dbo.Sales
的标识重置为1。但是最后一个查询为 Max_ID_Value 和 Current_Seed_Value 返回了不同的值。任何人都可以解释为什么DBCC命令在程序内部无效?
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
答案 0 :(得分:3)
很抱歉回答我自己的问题。正如@Kannan Kandasamy指出的那样,ROLLBACK TRANSACTION
代码正在恢复DBCC CHECKIDENT
所完成的工作。因此,为了使其工作,我创建了一个名为Reseed_Sales
的作业,其中包含表dbo.Sales
的RESEED Identity的代码。以下是程序usp_mytran
的最终查询。
-- Procedure to Populate data into dbo.Sales
ALTER 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')
EXEC msdb..sp_start_job @job_name = 'Reseed_Sales'
ROLLBACK TRANSACTION
END
答案 1 :(得分:1)
实际上它在存储过程中按预期工作:Rollback tran将回滚checkident值 - 这就是代码中发生的事情
-- Procedure to Populate data into dbo.Sales
alter 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('test.dbo.Sales')
DBCC CHECKIDENT ( 'test.dbo.Sales', RESEED, @MaxValue );
ROLLBACK TRANSACTION
END
如果你现在检查ident_current它显示2并且在下一次运行时它显示4等因为 ROLLBACK TRANSACTION
现在在多次执行后检查&#34; EXEC usp_mytran&#34;
select IDENT_CURRENT('test.dbo.Sales')
您将看到checkident不会重置。
如果我们删除该事务,则CHECKIDENT将重新设置为1。
以下评论的交易
-- Procedure to Populate data into dbo.Sales
alter 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('test.dbo.Sales')
DBCC CHECKIDENT ( 'test.dbo.Sales', RESEED, @MaxValue );
--ROLLBACK TRANSACTION
END
现在在多次执行后检查&#34; EXEC usp_mytran&#34;
select IDENT_CURRENT('test.dbo.Sales')
您会看到重新设定的值为&#39; 1&#39;
检查以下示例......