Sunday, July 8, 2012

Reset Table Identity

DBCC CHECKIDENT can reset the identity value of the table. For example, YourTable has 25 rows  with 25 as last identity and If we want next record to have identity other than 26.

OR

If you had truncated a table and reloaded, the Identity value will start from the previos value i.e YourTable has 25 rows  and you had truncated and reloaded now your identity column values will be from 25 - 50 for the 25 records you inserted.

Run following T SQL script to reset or reseed the identity value of the table:
DBCC CHECKIDENT ("dbo.TableName", RESEED, 1);

If table has to start with an identity of 1 with the next insert then table should be reseeded with the identity to 1. If identity seed is set below values that currently are in table, it will violate the uniqueness constraint as soon as the values start to duplicate and will generate error.

Wednesday, July 4, 2012

Stored Procedure to raise a message

CREATE PROCEDURE [dbo].[usp_Raise_Message]
@strMessage NVARCHAR(MAX) = NULL -- Message to display
AS
BEGIN
SET NOCOUNT ON
SET @strMessage = CONVERT(NVARCHAR(100),GETDATE()) + ' : ' + ISNULL(@strMessage,'No Message Raised')
RAISERROR(@strMessage,0,1) WITH NOWAIT
END