I have a bunch of LU tables that have values added and deleted all the time. Therefore, I figured writing a quick stored procedure would serve me well and then can call the stored procedure on another stored procedure that deletes values from the LU tables and then reseeds the ID
column directly after.
Stored procedure code
Create Procedure DBA.ReseedIdentity
@TableName nvarchar(20) = NULL,
@IdentityColumn nvarchar(25) = NULL
AS BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
IF (@TableName is null or @IdentityColumn is null)
BEGIN
RAISERROR('You Must Specify a Table AND the ID Column from that Table to Reseed the Identity!', 16,1)
RETURN;
END
Declare @MaxID int
set @MaxID = (SELECT ISNULL(MAX(@IdentityColumn),0) FROM @TableName)
DBCC CHECKIDENT(@TableName, RESEED, @MaxID)
END
I wanted to know if there is any trick to be able to pass the @TableName
parameter following the FROM
clause without dynamically creating it. I am new to writing stored procedures, so please forgive me if the answer is obvious.
Any help is greatly appreciated.