Monday 23 October 2023

Enable Disable Identity Field to migrate data

CREATE TABLE [dbo].[AnIdentityTable](

       [Id] [int] IDENTITY(1,1) NOT NULL,

       [_Name] [nvarchar](50) NOT NULL,

 CONSTRAINT [PK_AnIdentityTable] PRIMARY KEY CLUSTERED

(

       [Id] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

GO

 

-- Enable insert values in the Identity Field

SET IDENTITY_INSERT [dbo].[AnIdentityTable] ON;

 

INSERT INTO [dbo].[AnIdentityTable] (Id, _Name)

VALUES (5, 'Some Value');

 

-- Disable insert values in the Identity Field, after disabling it starts frm the max value

SET IDENTITY_INSERT [dbo].[AnIdentityTable] OFF; 

No comments:

Post a Comment