Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 get n-th level of foreign key inforamtion

Author  Topic 

maifs
Yak Posting Veteran

57 Posts

Posted - 2010-04-29 : 06:47:35
Hi..
how can i fetch n-th level of all foreign key's information?
for example: one table is 'A', second table name is 'B', third tables is 'C' and so on
table 'A' has a primary key, table 'B' has foreign key against table 'A' and
table 'C' has foreign key of table 'B' and so on..
i want to make a such dynamic script which can get all/n-th level of foreign key relation inofrmations against.
i made below script which can get foreign key information dynamically but it is used for single level not for n-th level.
i am using sql server 2005.


ALTER PROC [dbo].[sp_sng_fetching_foreign_key_info] @p_table_name varchar(100) , @flag bit
AS

DECLARE @pk_table_nme VARCHAR(150)
DECLARE @fk_table_nme VARCHAR(150)
DECLARE @pk_col_nme VARCHAR(150)
DECLARE @fk_col_nme VARCHAR(150)
DECLARE @constraint_nme VARCHAR(150)
DECLARE @flg bit

BEGIN
set @flg = @flag


DECLARE GETTING_ALL_FOREIGN_KEY_DATA CURSOR FOR
select ISNULL(c.constraint_name,''),

ISNULL(cfk.table_name ,''),

ISNULL(kcu.column_name ,''),

ISNULL(cpk.table_name ,''),

ISNULL(pkt.column_name ,'')


from information_schema.REFERENTIAL_CONSTRAINTS c

INNER JOIN information_schema.TABLE_CONSTRAINTS cpk on c.unique_constraint_name = cpk.constraint_name

INNER JOIN information_schema.TABLE_CONSTRAINTS cfk on c.constraint_name = cfk.constraint_name

INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON c.constraint_name = kcu.constraint_name

INNER JOIN
(
SELECT tci.table_name, kcui.column_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tci
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcui on tci.constraint_name = kcui.constraint_name
WHERE tci.CONSTRAINT_TYPE = 'PRIMARY KEY'

) PKT ON PKT.TABLE_NAME = CPK.TABLE_NAME
WHERE cpk.table_name = (@p_table_name)
ORDER BY 1,2,3,4




OPEN GETTING_ALL_FOREIGN_KEY_DATA


FETCH NEXT FROM GETTING_ALL_FOREIGN_KEY_DATA INTO @constraint_nme,@fk_table_nme ,@fk_col_nme, @pk_table_nme , @pk_col_nme

WHILE @@FETCH_STATUS = 0

BEGIN

IF @flg = 1
BEGIN
EXEC('ALTER TABLE [dbo].['+ @fk_table_nme +'] DROP CONSTRAINT ['+ @constraint_nme +']');
EXEC('ALTER TABLE [dbo].['+@fk_table_nme+'] WITH NOCHECK ADD CONSTRAINT ['+@constraint_nme+'] FOREIGN KEY(['+@fk_col_nme+'])
REFERENCES [dbo].['+ @pk_table_nme +'] (['+@pk_col_nme+']) ON DELETE CASCADE');
print 'Disabled Foreign key Constraints'
END
ELSE
print @flg


FETCH NEXT FROM GETTING_ALL_FOREIGN_KEY_DATA INTO @constraint_nme,@fk_table_nme ,@fk_col_nme, @pk_table_nme , @pk_col_nme
END



CLOSE GETTING_ALL_FOREIGN_KEY_DATA
DEALLOCATE GETTING_ALL_FOREIGN_KEY_DATA




END



Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-29 : 07:31:22
I wrote this a while ago when I was trying to work out what was happening in a 3rd party package. this only works for 2005 and later but builds up a recursive search of relationships. I've only coded it to work with single relationships (no compound keys) but it may work for you. It's pretty self explanatory

/*** (Key Cascade Report) *****************************************************
*
* Charlie (2010-April-20)
*
* Set @tableName to the name of the Root table you want to chart
* Dependancies for. leave NULL for all tables.
*
******************************************************************************/

DECLARE @tableName VARCHAR(255) SET @tableName = 'Employee'

/*****************************************************************************/

SET NOCOUNT ON

; WITH keyCas (
[baseTable]
, [tableName]
, [tableId]
, [keyname]
, [Key Column]
, [Target Column]
, [level]
, [tablePath]
, [tableIdPath]
)
AS (
-- Anchor Definition
SELECT
tbl.[name]
, tbl.[name]
, tbl.[object_ID]
, CAST('' AS VARCHAR(2000))
, CAST('' AS VARCHAR(2000))
, CAST('' AS VARCHAR(2000))
, 0
, CAST(tbl.[name] AS VARCHAR(MAX))
, CAST(tbl.[object_ID] AS VARCHAR(MAX))
FROM
sys.objects tbl
WHERE
[type] = 'U'
AND (
[name] = @tableName
OR
@tableName IS NULL
)

-- Recursive Defintion
UNION ALL SELECT
kc.[baseTable]
, tbl.[name]
, tbl.[object_Id]
, CAST(ky.[name] AS VARCHAR(2000))
, CAST(QUOTENAME(icp.[name]) AS VARCHAR(2000))
, CAST(QUOTENAME(kc.[tableName]) + '.' + QUOTENAME(icT.[name]) AS VARCHAR(2000))
, kc.[level] + 1
, kc.[tablePath] + CAST(' -> ' + tbl.[name] AS VARCHAR(MAX))
, kc.[tableIdPath] + CAST(',' + CAST(tbl.[object_ID] AS VARCHAR(255)) AS VARCHAR(MAX))
FROM
sys.foreign_key_columns fkc
JOIN sys.objects ky ON ky.[object_Id] = fkc.[constraint_object_id]
JOIN sys.objects tbl ON tbl.[object_id] = fkc.[parent_object_Id]
JOIN sys.objects target ON target.[object_ID] = fkc.[referenced_object_Id]

JOIN sys.columns icp ON
icp.[object_ID] = fkc.[parent_object_Id]
AND icp.[column_Id] = fkc.[parent_column_Id]

JOIN sys.columns icT ON
icT.[object_ID] = fkc.[referenced_object_Id]
AND icT.[column_Id] = fkc.[referenced_column_Id]

JOIN keyCas kc ON target.[object_ID] = kc.[tableId]
WHERE
kc.[tableIdPath] NOT LIKE '%' + CAST(tbl.[object_Id] AS VARCHAR(255)) + '%'
)
SELECT
[baseTable] AS [Root Table]
, [tableName] AS [Leaf Table]
, [keyname] AS [Key]
, [Key Column]
, [Target Column]
, [tablePath] AS [Dependancy List]
FROM
keyCas
ORDER BY
[tablePath]

OPTION (MAXRECURSION 1000)




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

maifs
Yak Posting Veteran

57 Posts

Posted - 2010-04-30 : 01:33:19
when i am trying with script then it throws some errors like:

Server: Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'WITH'.
Server: Msg 170, Level 15, State 1, Line 28
Line 28: Incorrect syntax near 'MAX'.
Server: Msg 170, Level 15, State 1, Line 49
Line 49: Incorrect syntax near 'MAX'.
Server: Msg 170, Level 15, State 1, Line 81
Line 81: Incorrect syntax near 'MAXRECURSION'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-30 : 02:24:22
working fine for me once you declare and set @tablename variable. Are you using sql 2005 atleast? what does below return you?

SELECT @@VERSION


EXEC sp_dbcmptlevel 'yourdbnamehere'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-30 : 04:15:36
quote:
Originally posted by maifs

when i am trying with script then it throws some errors like:

Server: Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'WITH'.
Server: Msg 170, Level 15, State 1, Line 28
Line 28: Incorrect syntax near 'MAX'.
Server: Msg 170, Level 15, State 1, Line 49
Line 49: Incorrect syntax near 'MAX'.
Server: Msg 170, Level 15, State 1, Line 81
Line 81: Incorrect syntax near 'MAXRECURSION'.



Sounds like you are either running in compatibility mode 80 or are not on 2005 at all.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

maifs
Yak Posting Veteran

57 Posts

Posted - 2010-04-30 : 06:07:05
version is this:
Microsoft SQL Server 2000 - 8.00.760 (Intel X86) Dec 17 2002 14:22:05 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
but i am using sql server 2005.
if this is ms server 2000 then how can i convert it into 2005 without any loss of data?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-30 : 06:37:15
quote:
Originally posted by maifs

version is this:
Microsoft SQL Server 2000 - 8.00.760 (Intel X86) Dec 17 2002 14:22:05 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
but i am using sql server 2005.
if this is ms server 2000 then how can i convert it into 2005 without any loss of data?


Well you aren't using sql server 2005 then. The database server is running sql server 2000 and that's all that matters (I guess that you are connecting through the 2005 client tools (management studio)).

To migrate the database to 2005 you'd have to back it up and restore that backup onto a 2005 database server. However that would then run in compatibility mode 80 (so using sql server 2000 syntax). You would change the compatibility mode to 90 (2005).

However, there are differences between the way that compat levels 80 and 90 work so it's not an operation you can do lightly.

On 2000 the VARCHAR(MAX) datatype doesn't exist and the CTE doesn't exist.

You could completely rewire the script to use a while loop and a temp table. Also, you never know but someone with more time than myself may do it for you.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -