Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Generate PKs

Author  Topic 

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2002-11-20 : 02:45:15
Here's another script I used to build the PK scripts based on the settings in a current database. Much thanks to Rob Volk who helped me track down some related posts and articles here on SQLTeam upon which this is based.

Two caveats[: 1) Be sure to output to Text and change the output options to return more than 256 characters. I used 2000 to be safe. 2) When a table has a multi-part key, this script does not necessarily list them in the proper order. We only have a few of those so I did them by hand rather than spend the time to figure out how to sort them. Anyone who wants to add that to this thread, please do so.


----------------------------------------------------------------------------------------
SET NOCOUNT ON
-- Create a Table Variable for work
declare @Table table(
TableName varchar(200) not null primary key,
ConstraintName varchar(200) not null,
ColumnList varchar(1000) null
)
-- Populate work table with alphabetized list of tables.
Insert @Table (TableName, ConstraintName)
SELECT DISTINCT TABLE_NAME, CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE
CONSTRAINT_NAME LIKE 'PK_%'
AND TABLE_NAME <> 'dtproperties'
ORDER BY TABLE_NAME

-- Do some magic here to build the list of primary key fields
declare @Keywords varchar(1000)

while @@RowCount > 0
begin
Set @Keywords = null
Select @Keywords = Coalesce(@Keywords + ', ', '') + COLUMN_NAME
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE K
Where
CONSTRAINT_NAME LIKE 'PK_%'
AND TABLE_NAME =
(
Select min(TableName) as TableName
from @Table
where ColumnList is null
)

Update @Table
set
ColumnList = @Keywords
from @Table T
where T.TableName = (Select min(TableName) as TableName from @Table where ColumnList is null)
end

SET NOCOUNT OFF
-- Output the final results we're looking for.
Select
'----------------------------------------------------------------------------------------------------
-- This script creates the Primary Key constraint for the ' + TableName + ' table.
--
-- changed by Date Comments
-- ---------- -------- ------------------------------------------------------------------------
-- AjarnMark 11/15/02 Script generated from existing database.
--
----------------------------------------------------------------------------------------------------
IF OBJECTPROPERTY(OBJECT_ID(N''' + ConstraintName + '''), ''IsPrimaryKey'') = 1 BEGIN
ALTER TABLE ' + TableName + ' DROP CONSTRAINT ' + ConstraintName + '
PRINT '' -- DRP - ' + ConstraintName + '''
END
G'+'O

ALTER TABLE ' + TableName + '
ADD CONSTRAINT ' + ConstraintName + '
PRIMARY KEY CLUSTERED (' + ColumnList + ')
PRINT '' -- ADD - ' + ConstraintName + '''
G'+'O

'
from @Table



Edited by - AjarnMark on 11/20/2002 02:46:49
   

- Advertisement -