<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; 2005 &#8211; Find Tables With Foreign Key Constraint in Database &#8211; Part 2</title>
	<atom:link href="http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #028 &#124; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-472978</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #028 &#124; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 11 May 2013 01:31:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-472978</guid>
		<description><![CDATA[[&#8230;] Find Tables With Foreign Key Constraint in Database [&#8230;]]]></description>
		<content:encoded><![CDATA[<p>[&#8230;] Find Tables With Foreign Key Constraint in Database [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Beginning New Weekly Series &#8211; Memory Lane &#8211; #001 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-368281</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Beginning New Weekly Series &#8211; Memory Lane &#8211; #001 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 03 Nov 2012 01:30:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-368281</guid>
		<description><![CDATA[[...] Version 2: Using sys.schema and additional columns [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Version 2: Using sys.schema and additional columns [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-187681</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Thu, 03 Nov 2011 08:45:39 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-187681</guid>
		<description><![CDATA[Refer this method for accuracy
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/truncate-all-tables-part-ii.aspx]]></description>
		<content:encoded><![CDATA[<p>Refer this method for accuracy<br />
<a href="http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/truncate-all-tables-part-ii.aspx" rel="nofollow">http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/truncate-all-tables-part-ii.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ech0</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-183388</link>
		<dc:creator><![CDATA[ech0]]></dc:creator>
		<pubDate>Tue, 25 Oct 2011 15:13:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-183388</guid>
		<description><![CDATA[I am new to this but I gave it a try anyway. 

I checked whether it was possible to truncate all tables of a database by using a cursor. However, since in most cases there are constraints between the tables, the cursor won&#039;t be able to work.



DECLARE @table varchar(150)

DECLARE db_console CURSOR FOR

select distinct TABLE_SCHEMA + &#039;.&#039; + TABLE_NAME from INFORMATION_SCHEMA.COLUMNS 


OPEN db_console 

fetch next from db_console into @table

while @@fetch_status = 0
	begin
		use AdventureWorks
		exec(&#039;truncate table &#039; + @table)
		--print @db + &#039;.&#039; + &#039;.&#039; + @schema + &#039;.&#039; + @table + &#039;truncated&#039; 
		fetch next from db_console into @table
	end
	
close db_console
deallocate db_console


You might as well say that this cursor is useless and in the same is a waste of time due to the fact that there is a stored procedure that can do that for us. 

According to a response from kristof (stackoverflow), I was able to track the following:

-- disable all constraints
EXEC sp_msforeachtable &quot;ALTER TABLE ? NOCHECK CONSTRAINT all&quot;

-- delete data in all tables
EXEC sp_MSForEachTable &quot;DELETE FROM ?&quot;

-- enable all constraints
exec sp_msforeachtable &quot;ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all&quot;

-- reseed tables
EXEC sp_MSforeachtable &quot;DBCC CHECKIDENT ( &#039;?&#039;, RESEED, 0)&quot;]]></description>
		<content:encoded><![CDATA[<p>I am new to this but I gave it a try anyway. </p>
<p>I checked whether it was possible to truncate all tables of a database by using a cursor. However, since in most cases there are constraints between the tables, the cursor won&#8217;t be able to work.</p>
<p>DECLARE @table varchar(150)</p>
<p>DECLARE db_console CURSOR FOR</p>
<p>select distinct TABLE_SCHEMA + &#8216;.&#8217; + TABLE_NAME from INFORMATION_SCHEMA.COLUMNS </p>
<p>OPEN db_console </p>
<p>fetch next from db_console into @table</p>
<p>while @@fetch_status = 0<br />
	begin<br />
		use AdventureWorks<br />
		exec(&#8216;truncate table &#8216; + @table)<br />
		&#8211;print @db + &#8216;.&#8217; + &#8216;.&#8217; + @schema + &#8216;.&#8217; + @table + &#8216;truncated&#8217;<br />
		fetch next from db_console into @table<br />
	end</p>
<p>close db_console<br />
deallocate db_console</p>
<p>You might as well say that this cursor is useless and in the same is a waste of time due to the fact that there is a stored procedure that can do that for us. </p>
<p>According to a response from kristof (stackoverflow), I was able to track the following:</p>
<p>&#8211; disable all constraints<br />
EXEC sp_msforeachtable &#8220;ALTER TABLE ? NOCHECK CONSTRAINT all&#8221;</p>
<p>&#8211; delete data in all tables<br />
EXEC sp_MSForEachTable &#8220;DELETE FROM ?&#8221;</p>
<p>&#8211; enable all constraints<br />
exec sp_msforeachtable &#8220;ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all&#8221;</p>
<p>&#8211; reseed tables<br />
EXEC sp_MSforeachtable &#8220;DBCC CHECKIDENT ( &#8216;?&#8217;, RESEED, 0)&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rohit Dadsena</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-45878</link>
		<dc:creator><![CDATA[Rohit Dadsena]]></dc:creator>
		<pubDate>Thu, 22 Jan 2009 06:17:30 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-45878</guid>
		<description><![CDATA[But How can i Truncate my Database(or All Tables From Dataabase)]]></description>
		<content:encoded><![CDATA[<p>But How can i Truncate my Database(or All Tables From Dataabase)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rohit Dadsena</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-45877</link>
		<dc:creator><![CDATA[Rohit Dadsena]]></dc:creator>
		<pubDate>Thu, 22 Jan 2009 06:08:28 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-45877</guid>
		<description><![CDATA[U really saved me lot of time.]]></description>
		<content:encoded><![CDATA[<p>U really saved me lot of time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Todd Owens</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-44892</link>
		<dc:creator><![CDATA[Todd Owens]]></dc:creator>
		<pubDate>Wed, 17 Dec 2008 20:12:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-44892</guid>
		<description><![CDATA[Bob Shultz:

Schema Names added below 

USE AdventureWorks
GO
SELECT 
f.name AS ForeignKey, 
SCHEMA_NAME(o.schema_id) AS SchemaName,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
f.update_referential_action_desc AS UpdateAction,
f.delete_referential_action_desc AS DeleteAction
FROM sys.foreign_keys AS f
JOIN sys.foreign_key_columns AS fc
    ON f.OBJECT_ID = fc.constraint_object_id
JOIN sys.objects o
    ON f.parent_object_id = o.object_id
ORDER BY 
SchemaName ASC,
TableName ASC, 
ColumnName ASC]]></description>
		<content:encoded><![CDATA[<p>Bob Shultz:</p>
<p>Schema Names added below </p>
<p>USE AdventureWorks<br />
GO<br />
SELECT<br />
f.name AS ForeignKey,<br />
SCHEMA_NAME(o.schema_id) AS SchemaName,<br />
OBJECT_NAME(f.parent_object_id) AS TableName,<br />
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,<br />
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,<br />
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,<br />
f.update_referential_action_desc AS UpdateAction,<br />
f.delete_referential_action_desc AS DeleteAction<br />
FROM sys.foreign_keys AS f<br />
JOIN sys.foreign_key_columns AS fc<br />
    ON f.OBJECT_ID = fc.constraint_object_id<br />
JOIN sys.objects o<br />
    ON f.parent_object_id = o.object_id<br />
ORDER BY<br />
SchemaName ASC,<br />
TableName ASC,<br />
ColumnName ASC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Schultz</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-41649</link>
		<dc:creator><![CDATA[Bob Schultz]]></dc:creator>
		<pubDate>Wed, 13 Aug 2008 16:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-41649</guid>
		<description><![CDATA[Pinal,  

This article was a great help to me.  

I was wondering if you could add schema columns to the result set?

Thanks again..

Bob]]></description>
		<content:encoded><![CDATA[<p>Pinal,  </p>
<p>This article was a great help to me.  </p>
<p>I was wondering if you could add schema columns to the result set?</p>
<p>Thanks again..</p>
<p>Bob</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan</title>
		<link>http://blog.sqlauthority.com/2008/05/08/sql-server-2005-find-tables-with-foreign-key-constraint-in-database-part-2/#comment-39610</link>
		<dc:creator><![CDATA[Ivan]]></dc:creator>
		<pubDate>Mon, 30 Jun 2008 20:20:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=597#comment-39610</guid>
		<description><![CDATA[thanks a lot, you saved me so much time!]]></description>
		<content:encoded><![CDATA[<p>thanks a lot, you saved me so much time!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
