I have a database with over 600 tables. Some of the tables have primary keys and some do not. How can I dynamically:
1. loop through all tables
2. select ones that do not have primary keys
3. add an auto incremented field
4. make this field the primary key
I would presume it would be a combination of the following:
USE MyDataBase;
GO
-- List all tables without primary constraint
SELECT i.name AS IndexName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 0
GO
-- add new auto incremented field
ALTER TABLE MyTable
ADD PK_ID BIGINT IDENTITY;
GO
-- create new primary key constraint
ALTER TABLE MyTable
ADD CONSTRAINT PK_ID PRIMARY KEY NONCLUSTERED (PK_ID);
GO
This query, will exclude all tables that have either a primary key or an identity column, it will then add identity column and primary key on the remaining objects
For the tables that already have identity column defined, you might use this query to set this identity column to be the primary key (because you can't have two identity columns on the same table)