Does anyone know how to write script to not only make a pre-existing and in use table column a primary key but also set the identity values?
I have inherited a database. It has a table being used, which is already populated with data. The table has an ID (int)(Not Null) field being used as a primary key, but it is NOT marked in SQL as a primary key. I have been able to write the following script to convert the ID table to a primary key field:
ALTER TABLE [dbo].[MYTABLE] WITH NOCHECK
ADD CONSTRAINT [PK_MYTABLE] PRIMARY KEY CLUSTERED
([ID]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
My main problem is I have NOT been able to figure out how to write the script to set the identity to YES with the identity seed set to 1 and the identity increment set to 1.
If it helps, the identity seed does not have to be 1. It can be set to anything. Also, if it helps, the identity information can be set before or with the primary key script.
I have inherited a database. It has a table being used, which is already populated with data. The table has an ID (int)(Not Null) field being used as a primary key, but it is NOT marked in SQL as a primary key. I have been able to write the following script to convert the ID table to a primary key field:
ALTER TABLE [dbo].[MYTABLE] WITH NOCHECK
ADD CONSTRAINT [PK_MYTABLE] PRIMARY KEY CLUSTERED
([ID]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
My main problem is I have NOT been able to figure out how to write the script to set the identity to YES with the identity seed set to 1 and the identity increment set to 1.
If it helps, the identity seed does not have to be 1. It can be set to anything. Also, if it helps, the identity information can be set before or with the primary key script.