WITH RefColumns AS
(
SELECT
C.referenced_object_id AS
[object_id],
C.parent_object_id,
STUFF((SELECT ', ' + QUOTENAME(B.name)
FROM sys.foreign_key_columns
A
JOIN sys.columns B ON B.[object_id] = A.referenced_object_id AND
B.column_id = A.referenced_column_id
WHERE C.parent_object_id = A.parent_object_id AND C.referenced_object_id
= A.referenced_object_id
FOR XML PATH('')), 1, 2, '') AS ColumnNames
FROM sys.foreign_key_columns
C
GROUP BY C.referenced_object_id,
C.parent_object_id
)
,ParentColumns AS
(
SELECT
C.parent_object_id AS [object_id],
C.referenced_object_id,
STUFF((SELECT ', ' + QUOTENAME(B.name)
FROM sys.foreign_key_columns
A
JOIN sys.columns B ON B.[object_id] = A.parent_object_id AND B.column_id = A.parent_column_id
WHERE C.parent_object_id = A.parent_object_id AND C.referenced_object_id
= A.referenced_object_id
FOR XML PATH('')), 1, 2, '') AS ColumnNames
FROM sys.foreign_key_columns
C
GROUP BY C.parent_object_id, C.referenced_object_id
)
SELECT
'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(PT.[schema_id])) + '.' + QUOTENAME(PT.name) + ' DROP CONSTRAINT'
+ ' ' + QUOTENAME(FK.name) AS [DropFKScript],
'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(PT.[schema_id])) + '.' + QUOTENAME(PT.name) + ' WITH CHECK ADD
CONSTRAINT '+ QUOTENAME(FK.name) + CHAR(13) + CHAR(10) +
'FOREIGN KEY(' + PC.ColumnNames + ')' + CHAR(13) + CHAR(10) +
'REFERENCES ' + QUOTENAME(SCHEMA_NAME(RT.[schema_id])) + '.' + QUOTENAME(RT.name) + ' (' + RC.ColumnNames + ')' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10) +
'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(PT.[schema_id])) + '.' + QUOTENAME(PT.name) + ' CHECK CONSTRAINT ' +
QUOTENAME(FK.name) + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
AS [CreateFKScript]
FROM sys.foreign_keys FK
JOIN sys.tables PT ON PT.[object_id] = FK.parent_object_id
JOIN ParentColumns AS PC ON PC.[object_id] = FK.parent_object_id AND PC.referenced_object_id
= FK.referenced_object_id
JOIN sys.tables RT ON RT.[object_id] = FK.referenced_object_id
JOIN RefColumns AS RC ON RC.[object_id] = FK.referenced_object_id
AND RC.parent_object_id = FK.parent_object_id
WHERE PT.name NOT IN ('dtproperties', 'sysdiagrams', '__RefactorLog')
AND RT.name NOT IN ('dtproperties', 'sysdiagrams', '__RefactorLog')
ORDER BY PT.name
GO
If anyone find any error or think this can be improved then please do post your comment.
Very helpful. Saved me some nightly hours.
ReplyDeleteThank You.
Clemens / Austria
Very Good Information. Thanks for this great share. The Script Component provides another area where programming logic can be applied in an SSIS package. This component, which can be used only in the Data Flow portion of an SSIS package, allows programmatic tasks to occur in the data stream. Each Script Task is called only once within a Control Flow, unless it is in a looping control. The Script Component has to be higher octane because it is going to be called per row of data in the data stream.
ReplyDeleteUsing the script component in SSIS
Quickbooks enterprise support Get Quickbooks Enterprise Support from certified Quickbooks Technical Team (+1 (833) 400-1001). We are available 24*7 for your service. Our Professional experts will resolve all issues of Quickbooks.(+1 (833) 400-1001)
ReplyDelete
ReplyDeleteQuickbooks enterprise support number Contact Quickbooks Enterprise Support Number to resolve any of your Quickbooks errors(+1 (833) 400-1001). Our Certified Quickbooks team is available 24*7. (+1 (833) 400-1001) to solve your Quickbooks issues.
ReplyDeleteQuickbooks enterprise support Phone number Solve your Quickbooks errors.Contact us and get 24*7 Quickbooks Enterprise support and get fixed Quickbooks errors by our certified Quickbooks Enterprise Support team. (+1 (833) 400-1001)