with pk as (select * from dbo.DbLayout where IsPkey = 1) ,other as (select * from dbo.DbLayout where IsPkey = 0) select pk.ColumnName ,PkTable = pk.TableName ,OnTable = o.TableName ,sql = 'alter table [' + o.TableName + '] with check add constraint [FK_' + o.TableName + '_' + pk.TableName + '] foreign key ([' + pk.ColumnName + ']) references [' + pk.TableName + '] ([' + pk.ColumnName + '])' from pk inner join other o on pk.ColumnName = o.ColumnName order by 1,2
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.