Michael J. Swart

May 10, 2010

Disowning Your Relatives

Filed under: SQL Scripts,SQLServerPedia Syndication,Technical Articles — Michael J. Swart @ 5:05 pm

Say that you’re a table, and you don’t want to refer to any parents or children using foreign keys (and vice versa).

There’s a script for that:

--drop fks coming in and out
DECLARE  @sql NVARCHAR (max)
SET @sql = '' 
SELECT @sql = @sql + N'ALTER TABLE ' + OBJECT_NAME(parent_object_id) +  N' DROP CONSTRAINT ' + name + N'; '
FROM sys.foreign_keys
WHERE 'TableName' IN (
    OBJECT_NAME(parent_object_id),
    OBJECT_NAME(referenced_object_id)
)
EXEC sp_executesql @sql

Just remember to replace “TableName” with the name of the table.

Disclaimer: Take care guys!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress