<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Save All Foreign Keys - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/save-all-foreign-keys/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Thu, 31 Mar 2022 18:08:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Save All Foreign Keys - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Making a Backup of All Foreign Keys of the Related Database in SQL Server</title>
		<link>https://mssqlquery.com/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Thu, 31 Mar 2022 18:08:34 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Backup of All Foreign Keys]]></category>
		<category><![CDATA[Save All Foreign Keys]]></category>
		<category><![CDATA[SQL Server Backup of All Foreign Keys]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=813</guid>

					<description><![CDATA[<p>Hello everyone, In this article, I will provide information on how to back up all Foreign Keys of the relevant database in SQL Server. In SQL Server, in some cases, you may want to back up all Foreign Keys of the relevant database. You can easily do this using the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server">Making a Backup of All Foreign Keys of the Related Database in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>In this article, I will provide information on how to back up all Foreign Keys of the relevant database in SQL Server.</p>
<p>In SQL Server, in some cases, you may want to back up all Foreign Keys of the relevant database.</p>
<p>You can easily do this using the code below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @schema_name AS SYSNAME;
DECLARE @table_name AS SYSNAME;
DECLARE @constraint_name AS SYSNAME;
DECLARE @constraint_object_id AS INT;
DECLARE @referenced_object_name AS SYSNAME;
DECLARE @is_disabled AS BIT;
DECLARE @is_not_for_replication AS BIT;
DECLARE @is_not_trusted AS BIT;
DECLARE @delete_referential_action AS TINYINT;
DECLARE @update_referential_action AS TINYINT;
DECLARE @tsql AS NVARCHAR (4000);
DECLARE @tsqlPK AS NVARCHAR (4000);
DECLARE @tsqlFK AS NVARCHAR (4000);
DECLARE @fkCol AS SYSNAME;
DECLARE @pkCol AS SYSNAME;
DECLARE @col1 AS BIT;
DECLARE @referenced_schema_name AS SYSNAME;

DECLARE FKcursor CURSOR
    FOR SELECT DISTINCT OBJECT_SCHEMA_NAME(f.parent_object_id),
                 OBJECT_NAME(f.parent_object_id),
                 name,
                 OBJECT_NAME(f.referenced_object_id),
                 OBJECT_ID,
                 is_disabled,
                 is_not_for_replication,
                 is_not_trusted,
                 delete_referential_action,
                 update_referential_action,
                 OBJECT_SCHEMA_NAME(f.referenced_object_id)
        FROM     sys.foreign_keys f WITH (NOLOCK)
        ORDER BY 1, 2;

OPEN FKcursor;

FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name, @referenced_object_name, @constraint_object_id, @is_disabled, @is_not_for_replication, 
								@is_not_trusted, @delete_referential_action, @update_referential_action, @referenced_schema_name;

WHILE @@FETCH_STATUS = 0
    BEGIN
        BEGIN
            SET @tsql = 'ALTER TABLE ' + QUOTENAME(@schema_name) + '.' + QUOTENAME(@table_name) + ' WITH CHECK ';

			SET @tsqlPK = '';
			SET @tsqlFK = '';
			DECLARE ColumnCursor CURSOR
				FOR SELECT   COL_NAME(fk.parent_object_id, fkc.parent_column_id),
								COL_NAME(fk.referenced_object_id, fkc.referenced_column_id)
					FROM     sys.foreign_keys AS fk WITH (NOLOCK)
								INNER JOIN
								sys.foreign_key_columns AS fkc WITH (NOLOCK)
								ON fk.[object_id] = fkc.constraint_object_id
					WHERE    fkc.constraint_object_id = @constraint_object_id
					ORDER BY fkc.constraint_column_id;
			OPEN ColumnCursor;
			SET @col1 = 1;
			FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;
			WHILE @@FETCH_STATUS = 0
				BEGIN
					IF (@col1 = 1)
					BEGIN
						SET @col1 = 0;
					END
					ELSE
					BEGIN
						SET @tsqlPK = @tsqlPK + ',';
						SET @tsqlFK = @tsqlFK + ',';
					END;

					SET @tsqlPK = @tsqlPK + QUOTENAME(@fkCol);
					SET @tsqlFK = @tsqlFK + QUOTENAME(@pkCol);

					FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;
				END;
			CLOSE ColumnCursor;
			DEALLOCATE ColumnCursor;

			SET @tsql = @tsql + ' ADD CONSTRAINT ' + QUOTENAME('FK_' + @table_name + '_' + @referenced_object_name +
				CASE 
					WHEN @tsqlPK = @tsqlFK  THEN '' 
					ELSE '_' + REPLACE(REPLACE(REPLACE(@tsqlPK, ',', '_'), ']', ''), '[', '')
				END
				) + ' FOREIGN KEY (' + @tsqlPK + ' ) ';
			SET @tsql = @tsql + ' REFERENCES ' + QUOTENAME(@referenced_schema_name) + '.' + QUOTENAME(@referenced_object_name) + ' (' + @tsqlFK + ')';
			SET @tsql = @tsql + ' ON UPDATE ' + CASE @update_referential_action 

			WHEN 0 THEN 'NO ACTION ' 
			WHEN 1 THEN 'CASCADE ' 
			WHEN 2 THEN 'SET NULL ' ELSE 'SET DEFAULT ' 
			END + ' ON DELETE ' + CASE @delete_referential_action 
			WHEN 0 THEN 'NO ACTION ' 
			WHEN 1 THEN 'CASCADE ' 
			WHEN 2 THEN 'SET NULL ' ELSE 'SET DEFAULT ' 
			END + CASE @is_not_for_replication 
			WHEN 1 THEN ' NOT FOR REPLICATION ' ELSE '' 
			END + ';';
        END;

        PRINT @tsql;

        FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name, @referenced_object_name, @constraint_object_id, @is_disabled, @is_not_for_replication, 
										@is_not_trusted, @delete_referential_action, @update_referential_action, @referenced_schema_name;
    END;

CLOSE FKcursor;
DEALLOCATE FKcursor;</code></pre>
<p>&nbsp;</p>
<p>When you run the above code on the relevant database, you will see a result similar to the one below.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-815 size-full" src="https://mssqlquery.com/wp-content/uploads/2022/03/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server-1.jpg" alt="Making a Backup of All Foreign Keys of the Related Database in SQL Server" width="700" height="392" srcset="https://mssqlquery.com/wp-content/uploads/2022/03/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2022/03/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server-1-300x168.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, all Foreign Key backups of the relevant database have been taken.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 173</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/making-a-backup-of-all-foreign-keys-of-the-related-database-in-sql-server">Making a Backup of All Foreign Keys of the Related Database in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
