robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-10-28 : 13:57:08
|
Be warned, index padding is not included in this, and I'm not sure the fillfactor setting is correct.set nocount oncreate table #PK(constraint_schema sysname not null, constraint_name sysname not null, sql varchar(4000) not null, constraint PK_#PK primary key clustered(constraint_schema, constraint_name))create table #cols(constraint_schema sysname not null, constraint_name sysname not null, column_name sysname not null, ordinal_position int not null, constraint PK_#PKcol primary key clustered(constraint_schema, constraint_name, ordinal_position))create table #FK(constraint_schema sysname not null, constraint_name sysname not null, unique_constraint_schema sysname not null, unique_constraint_name sysname not null,sql varchar(4000) not null, constraint PK_#FK primary key clustered(constraint_schema, constraint_name))insert into #PKselect constraint_schema, constraint_name, 'ALTER TABLE ' + quotename(table_schema) + '.' + quotename(TABLE_NAME) + ' ADD CONSTRAINT ' + quotename(CONSTRAINT_NAME) + ' PRIMARY KEY ' + CASE WHEN si.indid<>1 THEN 'NON' ELSE '' END + 'CLUSTERED (>cols<) WITH FILLFACTOR=' + cast(si.OrigFillFactor as varchar) + ' ON ' + quotename(fg.groupname)AS SQLfrom INFORMATION_SCHEMA.TABLE_CONSTRAINTS TCINNER JOIN sysindexes si on TC.CONSTRAINT_NAME=si.nameinner join sysfilegroups fg on si.groupid=fg.groupidWHERE CONSTRAINT_TYPE IN('PRIMARY KEY','UNIQUE')insert into #fkselect c.constraint_schema, c.constraint_name, c.unique_constraint_schema, c.unique_constraint_name,'ALTER TABLE ' + quotename(F.table_schema) + '.' + quotename(F.table_name) + ' ADD CONSTRAINT ' + quotename(F.constraint_name) + ' FOREIGN KEY(>cols<) REFERENCES ' + quotename(r.table_schema) + '.' + quotename(r.table_name) + '(>rcols<)'AS sqlFROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS F INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C ON F.constraint_schema=C.constraint_schema AND f.constraint_name=c.constraint_name AND F.constraint_type='FOREIGN KEY'INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS R ON R.constraint_schema=C.unique_constraint_schema AND r.constraint_name=c.unique_constraint_name AND r.constraint_type in ('PRIMARY KEY','UNIQUE')ORDER BY F.table_name, r.table_nameinsert into #colsselect constraint_schema, constraint_name, COLUMN_NAME, ORDINAL_POSITION from INFORMATION_SCHEMA.KEY_COLUMN_USAGEdeclare @ctr int, @max int, @delim varchar(1)select @ctr=1, @max=max(ordinal_position), @delim='' from #colsset nocount onwhile @ctr<=@maxBEGINupdate P SET SQL=Replace(SQL, '>cols<', @delim + quotename(c.column_name) + '>cols<')FROM #PK P INNER JOIN #cols C ON P.constraint_schema=C.constraint_schema AND P.constraint_name=C.constraint_nameWHERE C.ORDINAL_POSITION=@ctrUPDATE F SET SQL=Replace(Replace(SQL, '>cols<', @delim + quotename(c.column_name) + '>cols<'), '>rcols<', @delim + quotename(r.column_name) + '>rcols<')FROM #FK F INNER JOIN #cols C ON F.constraint_schema=C.constraint_schema AND F.constraint_name=C.constraint_name AND C.ordinal_position=@ctrINNER JOIN #cols R ON F.unique_constraint_schema=R.constraint_schema AND F.unique_constraint_name=R.constraint_name AND C.ordinal_position=R.ordinal_positionselect @ctr=@ctr+1, @delim=','ENDset nocount onupdate #PK SET SQL=Replace(SQL, '>cols<', '')update #FK SET SQL=Replace(Replace(SQL, '>cols<', ''), '>rcols<', '')select sql from #PKselect sql from #FKdrop table #pkdrop table #fkdrop table #cols |
|