<?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; Generate Foreign Key Scripts For Database</title>
	<atom:link href="http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Sun, 12 Feb 2012 09:22:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: dbidba</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-132552</link>
		<dc:creator><![CDATA[dbidba]]></dc:creator>
		<pubDate>Mon, 02 May 2011 20:51:39 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-132552</guid>
		<description><![CDATA[Thanks for posting this. Previously, I used a simpler script, but it does not handle foreign keys with multiple columns. I was dreading the more complex coding. This is being done to programatically accomplish the FK drops and creates during sliding window partition maintenance.]]></description>
		<content:encoded><![CDATA[<p>Thanks for posting this. Previously, I used a simpler script, but it does not handle foreign keys with multiple columns. I was dreading the more complex coding. This is being done to programatically accomplish the FK drops and creates during sliding window partition maintenance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dedogs</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-131972</link>
		<dc:creator><![CDATA[dedogs]]></dc:creator>
		<pubDate>Fri, 29 Apr 2011 14:54:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-131972</guid>
		<description><![CDATA[One more thing about my code... you have to output the results to text and then you have good formatting too.
Ow also thanks for the initial post. :)]]></description>
		<content:encoded><![CDATA[<p>One more thing about my code&#8230; you have to output the results to text and then you have good formatting too.<br />
Ow also thanks for the initial post. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dedogs</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-131971</link>
		<dc:creator><![CDATA[dedogs]]></dc:creator>
		<pubDate>Fri, 29 Apr 2011 14:50:53 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-131971</guid>
		<description><![CDATA[My version based off raknel&#039;s above formats the sql like MSMSS does. Good day!

SELECT
	&#039;ALTER TABLE dbo.[&#039; + object_name(fKeys.parent_object_id) + &#039;]&#039;
	+	CASE WHEN fKeys.is_not_trusted = 1
			THEN &#039; WITH NOCHECK&#039;
			ELSE &#039;&#039;
		END
	+	&#039; ADD CONSTRAINT [&#039; + fKeys.name + &#039;]&#039;
	+	&#039; FOREIGN KEY&#039; + char(13)
	+	&#039;(&#039;
	+	LEFT(
			(
				SELECT	pCols.name + &#039;, &#039; AS [text()]
				FROM sys.foreign_key_columns fKeyCols
				INNER JOIN sys.columns pCols	ON fKeyCols.referenced_column_id = pCols.column_id			AND fKeyCols.referenced_object_id = pCols.object_id
				WHERE fKeyCols.constraint_object_id = fKeys.object_id
				ORDER BY fKeyCols.referenced_column_id
				FOR XML PATH(&#039;&#039;)
			),
			LEN(
				(
					SELECT	pCols.name + &#039;, &#039; AS [text()]
					FROM sys.foreign_key_columns fKeyCols
					INNER JOIN sys.columns pCols	ON fKeyCols.referenced_column_id = pCols.column_id			AND fKeyCols.referenced_object_id = pCols.object_id
					WHERE fKeyCols.constraint_object_id = fKeys.object_id
					ORDER BY fKeyCols.referenced_column_id
					FOR XML PATH(&#039;&#039;)
				)
			) -1
		)
	+	&#039;) REFERENCES &#039; + object_name(fKeys.referenced_object_id) + char(13)
	+	&#039;(&#039;
	+	LEFT(
			(
				SELECT	rCols.name + &#039;, &#039; AS [text()]
				FROM sys.foreign_key_columns fKeyCols
				INNER JOIN sys.columns rCols	ON fKeyCols.parent_column_id = rCols.column_id			AND fKeyCols.parent_object_id = rCols.object_id
				WHERE fKeyCols.constraint_object_id = fKeys.object_id
				ORDER BY fKeyCols.referenced_column_id
				FOR XML PATH(&#039;&#039;)
			),
			LEN(
				(
					SELECT	rCols.name + &#039;, &#039; AS [text()]
					FROM sys.foreign_key_columns fKeyCols
					INNER JOIN sys.columns rCols	ON fKeyCols.parent_column_id = rCols.column_id			AND fKeyCols.parent_object_id = rCols.object_id
					WHERE fKeyCols.constraint_object_id = fKeys.object_id
					ORDER BY fKeyCols.referenced_column_id
					FOR XML PATH(&#039;&#039;)
				)
			) -1
		)
	+	&#039;)&#039; + char(13)
	+	&#039;GO&#039;
FROM
	(
		SELECT DISTINCT referenced_object_id, parent_object_id, object_id, name, is_not_trusted
		FROM sys.foreign_keys fKeys
		WHERE object_name(fKeys.referenced_object_id) = &#039;BOOKINGLINEISSUE&#039;
	) AS fKeys


]]></description>
		<content:encoded><![CDATA[<p>My version based off raknel&#8217;s above formats the sql like MSMSS does. Good day!</p>
<p>SELECT<br />
	&#8216;ALTER TABLE dbo.[' + object_name(fKeys.parent_object_id) + ']&#8216;<br />
	+	CASE WHEN fKeys.is_not_trusted = 1<br />
			THEN &#8216; WITH NOCHECK&#8217;<br />
			ELSE &#8221;<br />
		END<br />
	+	&#8216; ADD CONSTRAINT [' + fKeys.name + ']&#8216;<br />
	+	&#8216; FOREIGN KEY&#8217; + char(13)<br />
	+	&#8216;(&#8216;<br />
	+	LEFT(<br />
			(<br />
				SELECT	pCols.name + &#8216;, &#8216; AS [text()]<br />
				FROM sys.foreign_key_columns fKeyCols<br />
				INNER JOIN sys.columns pCols	ON fKeyCols.referenced_column_id = pCols.column_id			AND fKeyCols.referenced_object_id = pCols.object_id<br />
				WHERE fKeyCols.constraint_object_id = fKeys.object_id<br />
				ORDER BY fKeyCols.referenced_column_id<br />
				FOR XML PATH(&#8221;)<br />
			),<br />
			LEN(<br />
				(<br />
					SELECT	pCols.name + &#8216;, &#8216; AS [text()]<br />
					FROM sys.foreign_key_columns fKeyCols<br />
					INNER JOIN sys.columns pCols	ON fKeyCols.referenced_column_id = pCols.column_id			AND fKeyCols.referenced_object_id = pCols.object_id<br />
					WHERE fKeyCols.constraint_object_id = fKeys.object_id<br />
					ORDER BY fKeyCols.referenced_column_id<br />
					FOR XML PATH(&#8221;)<br />
				)<br />
			) -1<br />
		)<br />
	+	&#8216;) REFERENCES &#8216; + object_name(fKeys.referenced_object_id) + char(13)<br />
	+	&#8216;(&#8216;<br />
	+	LEFT(<br />
			(<br />
				SELECT	rCols.name + &#8216;, &#8216; AS [text()]<br />
				FROM sys.foreign_key_columns fKeyCols<br />
				INNER JOIN sys.columns rCols	ON fKeyCols.parent_column_id = rCols.column_id			AND fKeyCols.parent_object_id = rCols.object_id<br />
				WHERE fKeyCols.constraint_object_id = fKeys.object_id<br />
				ORDER BY fKeyCols.referenced_column_id<br />
				FOR XML PATH(&#8221;)<br />
			),<br />
			LEN(<br />
				(<br />
					SELECT	rCols.name + &#8216;, &#8216; AS [text()]<br />
					FROM sys.foreign_key_columns fKeyCols<br />
					INNER JOIN sys.columns rCols	ON fKeyCols.parent_column_id = rCols.column_id			AND fKeyCols.parent_object_id = rCols.object_id<br />
					WHERE fKeyCols.constraint_object_id = fKeys.object_id<br />
					ORDER BY fKeyCols.referenced_column_id<br />
					FOR XML PATH(&#8221;)<br />
				)<br />
			) -1<br />
		)<br />
	+	&#8216;)&#8217; + char(13)<br />
	+	&#8216;GO&#8217;<br />
FROM<br />
	(<br />
		SELECT DISTINCT referenced_object_id, parent_object_id, object_id, name, is_not_trusted<br />
		FROM sys.foreign_keys fKeys<br />
		WHERE object_name(fKeys.referenced_object_id) = &#8216;BOOKINGLINEISSUE&#8217;<br />
	) AS fKeys</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ram Kumar Gupta</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-104407</link>
		<dc:creator><![CDATA[Ram Kumar Gupta]]></dc:creator>
		<pubDate>Thu, 09 Dec 2010 07:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-104407</guid>
		<description><![CDATA[HI Seenivasan,

Thanks a lot for the procedure. It saved lots of my time.]]></description>
		<content:encoded><![CDATA[<p>HI Seenivasan,</p>
<p>Thanks a lot for the procedure. It saved lots of my time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-57678</link>
		<dc:creator><![CDATA[Jeff]]></dc:creator>
		<pubDate>Tue, 17 Nov 2009 23:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-57678</guid>
		<description><![CDATA[I like this script and can see where it can save me a lot of time.  However I can not get it to work on any schema other then dbo.  What change can I make to use scheams?]]></description>
		<content:encoded><![CDATA[<p>I like this script and can see where it can save me a lot of time.  However I can not get it to work on any schema other then dbo.  What change can I make to use scheams?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-52038</link>
		<dc:creator><![CDATA[Nathan]]></dc:creator>
		<pubDate>Tue, 19 May 2009 03:08:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-52038</guid>
		<description><![CDATA[Hey man...I appreciate this posting.

It saved me a LOT of time and headache.  A lot better than using the generate sql script wizard.

Thanks again.]]></description>
		<content:encoded><![CDATA[<p>Hey man&#8230;I appreciate this posting.</p>
<p>It saved me a LOT of time and headache.  A lot better than using the generate sql script wizard.</p>
<p>Thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Patel</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-50646</link>
		<dc:creator><![CDATA[Patel]]></dc:creator>
		<pubDate>Tue, 07 Apr 2009 21:33:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-50646</guid>
		<description><![CDATA[Raknel - Thanks for the excellent suggestion/script]]></description>
		<content:encoded><![CDATA[<p>Raknel &#8211; Thanks for the excellent suggestion/script</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raknel</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-43879</link>
		<dc:creator><![CDATA[raknel]]></dc:creator>
		<pubDate>Thu, 23 Oct 2008 15:16:44 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-43879</guid>
		<description><![CDATA[select  &#039;ALTER TABLE &#039;+object_name(a.parent_object_id)+
    case when a.is_not_trusted = 1 then &#039; WITH NOCHECK &#039;
        else &#039;&#039; end + 
	&#039; ADD CONSTRAINT &#039;+ a.name +
	&#039; FOREIGN KEY (&#039; + c.name + &#039;) REFERENCES &#039; +
	object_name(b.referenced_object_id) +
	&#039; (&#039; + d.name + &#039;)&#039;
from 	sys.foreign_keys a
     	join sys.foreign_key_columns b
                  on a.object_id=b.constraint_object_id
     	join sys.columns c
                  on b.parent_column_id = c.column_id
	         and a.parent_object_id=c.object_id
     	join sys.columns d
                  on b.referenced_column_id = d.column_id
	        and a.referenced_object_id = d.object_id
where   object_name(b.referenced_object_id) in
	(select name from sys.tables)
order by c.name]]></description>
		<content:encoded><![CDATA[<p>select  &#8216;ALTER TABLE &#8216;+object_name(a.parent_object_id)+<br />
    case when a.is_not_trusted = 1 then &#8216; WITH NOCHECK &#8216;<br />
        else &#8221; end +<br />
	&#8216; ADD CONSTRAINT &#8216;+ a.name +<br />
	&#8216; FOREIGN KEY (&#8216; + c.name + &#8216;) REFERENCES &#8216; +<br />
	object_name(b.referenced_object_id) +<br />
	&#8216; (&#8216; + d.name + &#8216;)&#8217;<br />
from 	sys.foreign_keys a<br />
     	join sys.foreign_key_columns b<br />
                  on a.object_id=b.constraint_object_id<br />
     	join sys.columns c<br />
                  on b.parent_column_id = c.column_id<br />
	         and a.parent_object_id=c.object_id<br />
     	join sys.columns d<br />
                  on b.referenced_column_id = d.column_id<br />
	        and a.referenced_object_id = d.object_id<br />
where   object_name(b.referenced_object_id) in<br />
	(select name from sys.tables)<br />
order by c.name</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shyam Bhavsar</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-41024</link>
		<dc:creator><![CDATA[Shyam Bhavsar]]></dc:creator>
		<pubDate>Wed, 30 Jul 2008 05:27:28 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-41024</guid>
		<description><![CDATA[hello friendz !!

I would like to know about foreign key reference in same database, my problem is - 

I have 2 tables
table 1 - 
CREATE TABLE [dbo].[Increment](
	[Org_Value_ID] [numeric](9, 0) NOT NULL,
	[Ph_Code] [numeric](9, 0) NOT NULL,
	[Sr_No] [int] NOT NULL,
	[Increment_S] [numeric](15,2) NULL,
	[Increment_V] [numeric](15,2) NULL,
CONSTRAINT [PK_Increment] PRIMARY KEY CLUSTERED 
(
	[Org_Value_ID] ASC,
	[Ph_Code] ASC,
	[Sr_No] ASC
)) ON [PRIMARY]


Table 2 - 
CREATE TABLE [dbo].[Org_Value](
	[Org_Value_ID] [numeric](9, 0) NOT NULL,
	[Ph_Code] [numeric](9, 0) NOT NULL,
	[Entitled] [numeric](9, 2) NULL,
 CONSTRAINT [PK_Org_Value] PRIMARY KEY CLUSTERED 
(
	[Org_Value_ID] ASC,
	[Ph_Code] ASC
)) ON [PRIMARY]

I want to add foreign key reference from Org_value to Increment on Org_value_id &amp; Ph_Code

when i tried - 
ALTER TABLE [dbo].[Org_Value]  WITH CHECK ADD  CONSTRAINT [FK_Org_Value_Increment] FOREIGN KEY([Org_value_ID], [Ph_Code]) REFERENCES [dbo].[Increment] ([Org_Value_ID],[Ph_Code])

I am getting error -
There are no primary or candidate keys in the referenced table &#039;dbo.Increment&#039; that match the referencing column list in the foreign key &#039;FK_Org_Value_Increment&#039;.


Please provide some comments, what should i do ?]]></description>
		<content:encoded><![CDATA[<p>hello friendz !!</p>
<p>I would like to know about foreign key reference in same database, my problem is &#8211; </p>
<p>I have 2 tables<br />
table 1 &#8211;<br />
CREATE TABLE [dbo].[Increment](<br />
	[Org_Value_ID] [numeric](9, 0) NOT NULL,<br />
	[Ph_Code] [numeric](9, 0) NOT NULL,<br />
	[Sr_No] [int] NOT NULL,<br />
	[Increment_S] [numeric](15,2) NULL,<br />
	[Increment_V] [numeric](15,2) NULL,<br />
CONSTRAINT [PK_Increment] PRIMARY KEY CLUSTERED<br />
(<br />
	[Org_Value_ID] ASC,<br />
	[Ph_Code] ASC,<br />
	[Sr_No] ASC<br />
)) ON [PRIMARY]</p>
<p>Table 2 &#8211;<br />
CREATE TABLE [dbo].[Org_Value](<br />
	[Org_Value_ID] [numeric](9, 0) NOT NULL,<br />
	[Ph_Code] [numeric](9, 0) NOT NULL,<br />
	[Entitled] [numeric](9, 2) NULL,<br />
 CONSTRAINT [PK_Org_Value] PRIMARY KEY CLUSTERED<br />
(<br />
	[Org_Value_ID] ASC,<br />
	[Ph_Code] ASC<br />
)) ON [PRIMARY]</p>
<p>I want to add foreign key reference from Org_value to Increment on Org_value_id &amp; Ph_Code</p>
<p>when i tried &#8211;<br />
ALTER TABLE [dbo].[Org_Value]  WITH CHECK ADD  CONSTRAINT [FK_Org_Value_Increment] FOREIGN KEY([Org_value_ID], [Ph_Code]) REFERENCES [dbo].[Increment] ([Org_Value_ID],[Ph_Code])</p>
<p>I am getting error -<br />
There are no primary or candidate keys in the referenced table &#8216;dbo.Increment&#8217; that match the referencing column list in the foreign key &#8216;FK_Org_Value_Increment&#8217;.</p>
<p>Please provide some comments, what should i do ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: doc_sewell</title>
		<link>http://blog.sqlauthority.com/2008/04/18/sql-server-generate-foreign-key-scripts-for-database/#comment-39302</link>
		<dc:creator><![CDATA[doc_sewell]]></dc:creator>
		<pubDate>Wed, 18 Jun 2008 02:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=573#comment-39302</guid>
		<description><![CDATA[so how come he says cusror instead of cursor?  What if I just want to show all the tables with FKs and the FK names and the status if they are enabled or disabled?]]></description>
		<content:encoded><![CDATA[<p>so how come he says cusror instead of cursor?  What if I just want to show all the tables with FKs and the FK names and the status if they are enabled or disabled?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

