drop table MetaPkFkRelations
SELECT OBJECT_NAME(f.parent_object_id) AS 'FkTable' ,
f.name AS 'FkName' ,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS 'FkColName' ,
OBJECT_NAME (f.referenced_object_id) as 'PkTable' , COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS 'PKColName'
into MetaPkFkRelations
FROM .sys.foreign_keys AS f
INNER JOIN .sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
--select * from MetaPkFkRelations
--exec procUtils_ListCols 'MetaPkFkRelations'
SELECT 'ALTER TABLE [' + [FkTable]+ '] ADD CONSTRAINT FK_' + [FkTable]+ '_' + [FkColName] + '_' +
PkTable + '_' + PkColName + ' FOREIGN KEY(' + FkColName + ')REFERENCES [' + PkTable +'](' + PkColName + ')' from MetaPkFkRelations
SELECT 'ALTER TABLE [' + FkTable + '] DROP CONSTRAINT FK_' + [FkTable]+ '_' + [FkColName] + '_' +
PkTable + '_' + PkColName from MetaPkFkRelations
--ALTER TABLE [UserRole] ADD CONSTRAINT FK_UserRole_UserRoleId_UserRole_UserRoleId FOREIGN KEY(UserRoleId)REFERENCES [UserRole](UserRoleId)
--ALTER TABLE [Bug] ADD CONSTRAINT FK_Bug_AssignedTo_User_UserId FOREIGN KEY(AssignedTo)REFERENCES [User](UserId)
--ALTER TABLE [Bug] ADD CONSTRAINT FK_Bug_DetectedBy_User_UserId FOREIGN KEY(DetectedBy)REFERENCES [User](UserId)
--ALTER TABLE [UserRole] DROP CONSTRAINT FK_UserRole_UserRoleId_UserRole_UserRoleId
--ALTER TABLE [Bug] DROP CONSTRAINT FK_Bug_AssignedTo_User_UserId
--ALTER TABLE [Bug] DROP CONSTRAINT FK_Bug_DetectedBy_User_UserId
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!