Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Retrieving foreign keys
Message
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Import/Export
Title:
Retrieving foreign keys
Miscellaneous
Thread ID:
00851171
Message ID:
00851171
Views:
53
Hi,

I want to script all the foreign keys in my data so that i can exporte them to a more recent version of Db but which as lost its foreign keys. I want to do this with having to rebuild all the DB. I was able to find a few information :
- foreign key name (fkname)
- source table (stname)
- source field (sfname)

by executing this query :
Select TableName.Name as stname, ConsName.Name as fkname, col.sfname
from sysobjects ConsName
inner join sysobjects TableName
on ConsName.Parent_obj = TableName.id
inner join sysconstraints cons
on cons.constid = ConsName.ID
inner join syscolumns col
on col.colid = cons.colid and col.id = TableName.id
where ConsName.xtype ='F'
order by stname, fkname

but i'm missing the destination table and field. with these missing pieces I'll be able to reconstruct the script :

ALTER TABLE dbo.stname WITH NOCHECK ADD CONSTRAINT fkname FOREIGN KEY
(sfname) REFERENCES dbo.dtname (ffid) NOT FOR REPLICATION
GO
ALTER TABLE dbo.stname NOCHECK CONSTRAINT fkname
GO

So the question is where does SQL keep the rest of the information on the foreign keys?

Alain
Analystik
Analystik Team
1430 Belanger
Montreal (Quebec)
Canada
(514) 278-2727
analyste@analystik.ca
Next
Reply
Map
View

Click here to load this message in the networking platform