Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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)

The Ultimate Foreign Key Researcher

By Jesse Roberge,

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
Util_FKSearch
By Jesse Roberge - YeshuaAgapao@Yahoo.com

Searches for actual and potental foreign key columns for a given primary key reference.
Generates code for creating, dropping, enabling, and disabling a FK constraint.
Also generates simple template code for merge and delete.
Also generates Util_FKSearch calls for the next lower dependency tiers.
@PKTable/@PKColumn must be a valid table/column combination or no data will be returned.
It doesn't check if @PKColumn is actually a member of a primary key or unique key constraint though.
It doesn't support composite keys, but if the one provided PK column is a member of a composite FK constraint,
it will consider it as having a FK constraint declared on it.
Requires the VIEW DATABASE STATE database permission.

Update 2009-09-21:
Added FindMode 0 to represent the PK table
Added pre-built Util_FKSearch call for the next lower tier of possible referential dependencies.
Added SimpleDelete, FKCreate, FKDrop, FKDisable, FKEnable code template output colunms

Required Input Parameters:
@PKTable VarChar(250), Table of the primary key reference
@PKColumn VarChar(250), Column of the primary key reference

Optional Input Parameters:
@PKSchema VarChar(250)='dbo', Schema of the primary key reference column's table
@FKColumn VarChar(250)='', If the FK columns might be named differently, provide this
@MaxFindMethod int How poor of a match level is permitted to be outputted

Usage:
EXECUTE Util_FKSearch @PKTable='orders', @PKColumn='order_id'
EXECUTE Util_FKSearch @PKSchema='dbo', @PKTable='orders', @PKColumn='order_id', @FKColumn='ORDERS_ORDER_ID'

Copyright:
Licensed under the L-GPL - a weak copyleft license - you are permitted to use this as a component of a proprietary database and call this from proprietary software.
Copyleft lets you do anything you want except plagiarize, conceal the source, or prohibit copying & re-distribution of this script/proc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

see <http://www.fsf.org/licensing/licenses/lgpl.html> for the license text.

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

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

Alter column order

Alter column order

FORUM

Write Query Dynamic Generated Column

Write Query Dynamic Generated Column

FORUM

Columns order in the index

Columns order in the index

ARTICLE

Column Order in an Index

This short article shows the importance of order of columns in an index.

BLOG

Column alias in order by and where

If you ever wonder why column alias can be used in order by and not in where clause, it’s the...

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones