Technical Article

Ensure Table Has Only 1 Row

,

The Recommended Approach does not require any line of coding efforts, and maintainance efforts.

Ensure A Table has only 1 row

Sometimes you have to create settings tables which only have one row. 
If you want to ensure table has only one row always and nobody can mess with it, follow the below technique

CREATE TABLE [dbo].[SingleRowTable](
[DummyPKID] [int] NOT NULL,
[ParamName] [nvarchar](100) NULL,
[ParamValue] [nvarchar](100) NULL,
PRIMARY KEY CLUSTERED 
(
[DummyPKID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[SingleRowTable]  WITH CHECK ADD  CONSTRAINT [CK_SingleRowTable] CHECK  (([DummyPKID]=(0)))
GO

ALTER TABLE [dbo].[SingleRowTable] CHECK CONSTRAINT [CK_SingleRowTable]
GO

ALTER TABLE [dbo].[SingleRowTable] ADD  DEFAULT ((0)) FOR [DummyPKID]
GO

Insert into SingleRowTable(ParamName,ParamValue) Values ('P1', 'P1 TEST')

Rate

2 (7)

You rated this post out of 5. Change rating

Share

Share

Rate

2 (7)

You rated this post out of 5. Change rating