SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Script to Generate Foreign Keys

By Sheraz Mirza,

This Script will Generate a Script as a ResultSet, which need to be run to create Foreign Keys, or could be saved for future use.

This is a useful script to Regenerate Foreign Key Constraint , if its been deleted accidently or on purpose, and situation can be critical if those constaints name are been used within our Application code, 

When you run the script it will give you 2 ResultSets, First one is information about fetched Foreign Keys , and other one is Alter Script to add those foreign keys in corresponding tables. 

You can save that Second ResultSet to use as a whole or as a part to recover any Foreign Key.

There is a drawback in the script, i.e For Composite Foreign Keys, it generates create command twice, which cause an error, which needs to be fix manually. In this case you need to delete second command and modify first one with appropriate syntax.

See you next time guys....

Total article views: 681 | Views in the last 30 days: 2
 
Related Articles
FORUM

SSIS Resetting the ResultSet in Script Task

SSIS Resetting the ResultSet in Script Task

SCRIPT

Script foreign keys

Using this script you can create a list of sql commands for foreign keys manipulation.

FORUM

Script to find Foreign Key missing Index?

Script to find Foreign Key missing Index?

FORUM

Resultset Size

Resultset Size

ARTICLE

Foreign Keys

Foreign keys are an important part of a relational database. New author Ravi Lobo takes a look at fo...

Tags
 
Contribute