<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Drop all foreign keys</title>
	<atom:link href="http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/</link>
	<description>Tom's notes of what is happening and the computers I use</description>
	<lastBuildDate>Mon, 16 Jan 2012 08:51:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Kristof Elst</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-47767</link>
		<dc:creator>Kristof Elst</dc:creator>
		<pubDate>Mon, 16 Jan 2012 08:51:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-47767</guid>
		<description>Thanks for your article.

Here&#039;s another way to do it, without resorting to a blighted cursor.

http://www.sentientbeings.com/2012/01/drop-all-foreign-keys-on-a-database-and-optionally-drop-all-tables/</description>
		<content:encoded><![CDATA[<p>Thanks for your article.</p>
<p>Here&#8217;s another way to do it, without resorting to a blighted cursor.</p>
<p><a href="http://www.sentientbeings.com/2012/01/drop-all-foreign-keys-on-a-database-and-optionally-drop-all-tables/" rel="nofollow">http://www.sentientbeings.com/2012/01/drop-all-foreign-keys-on-a-database-and-optionally-drop-all-tables/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rajendra.more999@hotmail.com</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-47396</link>
		<dc:creator>rajendra.more999@hotmail.com</dc:creator>
		<pubDate>Sat, 12 Nov 2011 11:07:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-47396</guid>
		<description>Thanks Really working ...</description>
		<content:encoded><![CDATA[<p>Thanks Really working &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-46830</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Sat, 17 Sep 2011 00:57:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-46830</guid>
		<description>Going to have try this script next time this problem comes up, thanks!</description>
		<content:encoded><![CDATA[<p>Going to have try this script next time this problem comes up, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noor</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-46342</link>
		<dc:creator>Noor</dc:creator>
		<pubDate>Fri, 15 Jul 2011 13:34:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-46342</guid>
		<description>Its better to disable foreign keys. If you want to disable all constraints in the database just run this code:
-- disable all constraints
EXEC sp_msforeachtable &quot;ALTER TABLE ? NOCHECK CONSTRAINT all&quot;

-- enable all constraints
exec sp_msforeachtable @command1=&quot;print &#039;?&#039;&quot;, @command2=&quot;ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all&quot;</description>
		<content:encoded><![CDATA[<p>Its better to disable foreign keys. If you want to disable all constraints in the database just run this code:<br />
&#8211; disable all constraints<br />
EXEC sp_msforeachtable &#8220;ALTER TABLE ? NOCHECK CONSTRAINT all&#8221;</p>
<p>&#8211; enable all constraints<br />
exec sp_msforeachtable @command1=&#8221;print &#8216;?&#8217;&#8221;, @command2=&#8221;ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arno</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-46337</link>
		<dc:creator>Arno</dc:creator>
		<pubDate>Wed, 13 Jul 2011 05:33:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-46337</guid>
		<description>Added some brackets for the &quot;weird&quot; table names:

--ENJOY
DECLARE @sqlForeignKeys VARCHAR(MAX)

SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,&#039;&#039;) +
&#039;ALTER TABLE dbo.[&#039; + OBJECT_NAME(FK.parent_object_id) + &#039;] DROP CONSTRAINT [&#039; + FK.name + &#039;];&#039; + CHAR(10)
FROM SYS.FOREIGN_KEYS FK

PRINT(@sqlForeignKeys)
--EXEC(@sqlForeignKeys)</description>
		<content:encoded><![CDATA[<p>Added some brackets for the &#8220;weird&#8221; table names:</p>
<p>&#8211;ENJOY<br />
DECLARE @sqlForeignKeys VARCHAR(MAX)</p>
<p>SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,&#8221;) +<br />
&#8216;ALTER TABLE dbo.[' + OBJECT_NAME(FK.parent_object_id) + '] DROP CONSTRAINT [' + FK.name + '];&#8217; + CHAR(10)<br />
FROM SYS.FOREIGN_KEYS FK</p>
<p>PRINT(@sqlForeignKeys)<br />
&#8211;EXEC(@sqlForeignKeys)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timmy</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-45703</link>
		<dc:creator>Timmy</dc:creator>
		<pubDate>Tue, 17 May 2011 07:42:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-45703</guid>
		<description>-- ENJOY
DECLARE @sqlForeignKeys VARCHAR(MAX)

SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,&#039;&#039;) +
	&#039;ALTER TABLE dbo.&#039; + OBJECT_NAME(FK.parent_object_id) + &#039; DROP CONSTRAINT &#039; + FK.name + &#039;;&#039; + CHAR(10)
FROM SYS.FOREIGN_KEYS FK

PRINT(@sqlForeignKeys)
--EXEC(@sqlForeignKeys)</description>
		<content:encoded><![CDATA[<p>&#8211; ENJOY<br />
DECLARE @sqlForeignKeys VARCHAR(MAX)</p>
<p>SELECT @sqlForeignKeys = ISNULL(@sqlForeignKeys,&#8221;) +<br />
	&#8216;ALTER TABLE dbo.&#8217; + OBJECT_NAME(FK.parent_object_id) + &#8216; DROP CONSTRAINT &#8216; + FK.name + &#8216;;&#8217; + CHAR(10)<br />
FROM SYS.FOREIGN_KEYS FK</p>
<p>PRINT(@sqlForeignKeys)<br />
&#8211;EXEC(@sqlForeignKeys)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chris</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-45510</link>
		<dc:creator>chris</dc:creator>
		<pubDate>Wed, 04 May 2011 02:36:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-45510</guid>
		<description>Combining ideas [1] and [7]

DECLARE @fkdel varchar(512)
DECLARE FkCrsr CURSOR FOR
	SELECT &#039;ALTER TABLE [&#039; + TABLE_SCHEMA + &#039;].[&#039; + TABLE_NAME +
	 &#039;] DROP CONSTRAINT [&#039; + CONSTRAINT_NAME +&#039;]&#039;
	 FROM information_schema.table_constraints
	 WHERE CONSTRAINT_TYPE = &#039;FOREIGN KEY&#039;

open FkCrsr
fetch next from FkCrsr into @fkdel
while @@FETCH_STATUS = 0
begin
	print @fkdel
	exec (@fkdel)
	fetch next from FkCrsr into @fkdel
end

close FkCrsr
deallocate FkCrsr
go</description>
		<content:encoded><![CDATA[<p>Combining ideas [1] and [7]</p>
<p>DECLARE @fkdel varchar(512)<br />
DECLARE FkCrsr CURSOR FOR<br />
	SELECT &#8216;ALTER TABLE [' + TABLE_SCHEMA + '].[' + TABLE_NAME +<br />
	 '] DROP CONSTRAINT [' + CONSTRAINT_NAME +']&#8216;<br />
	 FROM information_schema.table_constraints<br />
	 WHERE CONSTRAINT_TYPE = &#8216;FOREIGN KEY&#8217;</p>
<p>open FkCrsr<br />
fetch next from FkCrsr into @fkdel<br />
while @@FETCH_STATUS = 0<br />
begin<br />
	print @fkdel<br />
	exec (@fkdel)<br />
	fetch next from FkCrsr into @fkdel<br />
end</p>
<p>close FkCrsr<br />
deallocate FkCrsr<br />
go</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-43542</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Tue, 04 Jan 2011 21:54:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-43542</guid>
		<description>Sorry the last post was missing the  in the where and had the wrong quotes.

--DROP CONSTRAINTS
DECLARE FK_cursor CURSOR
  FOR
  SELECT S.name, F.name, T.name 
    FROM sys.foreign_keys F 
      JOIN sys.tables T ON F.parent_object_id=T.object_id  
      JOIN sys.schemas S ON S.schema_id=T.schema_id
    WHERE 
      T.name in (&#039;table1&#039;,&#039;table2&#039;,&#039;table3&#039;)
OPEN FK_cursor
DECLARE @shemaName sysname, @fkName sysname, @tableName sysname
FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName
WHILE (@@FETCH_STATUS  -1)
BEGIN
  PRINT &#039;Dropping constraint &#039; + @fkName + &#039; on table &#039; + @shemaName + &#039;.&#039; + @tableName
  EXECUTE (&#039;ALTER TABLE [&#039; + @shemaName + &#039;].[&#039; + @tableName + &#039;] DROP CONSTRAINT [&#039; + @fkName + &#039;]&#039;)
  FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName
END</description>
		<content:encoded><![CDATA[<p>Sorry the last post was missing the  in the where and had the wrong quotes.</p>
<p>&#8211;DROP CONSTRAINTS<br />
DECLARE FK_cursor CURSOR<br />
  FOR<br />
  SELECT S.name, F.name, T.name<br />
    FROM sys.foreign_keys F<br />
      JOIN sys.tables T ON F.parent_object_id=T.object_id<br />
      JOIN sys.schemas S ON S.schema_id=T.schema_id<br />
    WHERE<br />
      T.name in (&#8216;table1&#8242;,&#8217;table2&#8242;,&#8217;table3&#8242;)<br />
OPEN FK_cursor<br />
DECLARE @shemaName sysname, @fkName sysname, @tableName sysname<br />
FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName<br />
WHILE (@@FETCH_STATUS  -1)<br />
BEGIN<br />
  PRINT &#8216;Dropping constraint &#8216; + @fkName + &#8216; on table &#8216; + @shemaName + &#8216;.&#8217; + @tableName<br />
  EXECUTE (&#8216;ALTER TABLE [' + @shemaName + '].[' + @tableName + '] DROP CONSTRAINT [' + @fkName + ']&#8216;)<br />
  FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-43540</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Tue, 04 Jan 2011 21:47:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-43540</guid>
		<description>Why not take it a step further and execute the alter automatically.  Especially if it is part of a maintenance job that performs a truncate on the tables.

DECLARE FK_cursor CURSOR
  FOR
  SELECT S.name, F.name, T.name 
    FROM sys.foreign_keys F 
      JOIN sys.tables T ON F.parent_object_id=T.object_id  
      JOIN sys.schemas S ON S.schema_id=T.schema_id
    WHERE 
      T.name in (&#039;PayldSeg&#039;,&#039;PayldDest&#039;,&#039;Payld&#039;,&#039;DataPkgUsrStsHist&#039;,&#039;DataPkgUsr&#039;,&#039;DataPkg&#039;,&#039;DataPkgRqstQueUsr&#039;,&#039;ServPrcsMsg&#039;,&#039;DataPkgRqstQue&#039;)
OPEN FK_cursor
DECLARE @shemaName sysname, @fkName sysname, @tableName sysname
FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName
WHILE (@@FETCH_STATUS  -1)
BEGIN
  PRINT &#039;Dropping constraint &#039; + @fkName + &#039; on table &#039; + @shemaName + &#039;.&#039; + @tableName
  EXECUTE (&#039;ALTER TABLE [&#039; + @shemaName + &#039;].[&#039; + @tableName + &#039;] DROP CONSTRAINT [&#039; + @fkName + &#039;]&#039;)
  FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName
END</description>
		<content:encoded><![CDATA[<p>Why not take it a step further and execute the alter automatically.  Especially if it is part of a maintenance job that performs a truncate on the tables.</p>
<p>DECLARE FK_cursor CURSOR<br />
  FOR<br />
  SELECT S.name, F.name, T.name<br />
    FROM sys.foreign_keys F<br />
      JOIN sys.tables T ON F.parent_object_id=T.object_id<br />
      JOIN sys.schemas S ON S.schema_id=T.schema_id<br />
    WHERE<br />
      T.name in (&#8216;PayldSeg&#8217;,'PayldDest&#8217;,'Payld&#8217;,'DataPkgUsrStsHist&#8217;,'DataPkgUsr&#8217;,'DataPkg&#8217;,'DataPkgRqstQueUsr&#8217;,'ServPrcsMsg&#8217;,'DataPkgRqstQue&#8217;)<br />
OPEN FK_cursor<br />
DECLARE @shemaName sysname, @fkName sysname, @tableName sysname<br />
FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName<br />
WHILE (@@FETCH_STATUS  -1)<br />
BEGIN<br />
  PRINT &#8216;Dropping constraint &#8216; + @fkName + &#8216; on table &#8216; + @shemaName + &#8216;.&#8217; + @tableName<br />
  EXECUTE (&#8216;ALTER TABLE [' + @shemaName + '].[' + @tableName + '] DROP CONSTRAINT [' + @fkName + ']&#8216;)<br />
  FETCH NEXT FROM FK_cursor INTO @shemaName, @fkName, @tableName<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/comment-page-1/#comment-42972</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Wed, 24 Nov 2010 19:12:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.thestidhams.com/tom/wp/2007/07/23/drop-all-foreign-keys/#comment-42972</guid>
		<description>Good point, wrapping the names in brackets would avoid issues. I always avoid naming my tables such that it would be a problem, makes life ever so much easier but one cannot count on others to do the same.</description>
		<content:encoded><![CDATA[<p>Good point, wrapping the names in brackets would avoid issues. I always avoid naming my tables such that it would be a problem, makes life ever so much easier but one cannot count on others to do the same.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.323 seconds -->

