Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to query system tables for FK constraints
Message
From
04/06/2001 15:59:59
 
 
To
04/06/2001 14:04:28
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00514723
Message ID:
00514806
Views:
10
Since I was not able to do this in one query, I wrote a vfp routine using the system sp sp_fkeys to create my the sql strings. Here is what I did. I'd still lilke to know if this can be done in one query.
** create_fk_list.prg
** Uses SS sp_fkeys to generate sql code for re-creating FK's
LOCAL lnDSN, lcDSN, lcDSNUser, lcDSNPass, lcSQL, lnSuccess, lcString

lcDSN		= 'mydsn'
lcDSNUser	= 'user'
lcDSNPass	= 'password'

lnDSN = sqlcon(lcDSN, lcDSNUser, lcDSNPass)
IF lnDSN  < 0
	?'ERROR - sqlconnect()failed'
	return
ENDIF

IF USED('cTableList')
	USE IN cTableList
ENDIF

lnSuccess = sqltables(lnDSN, 'TABLE','cTableList')
IF lnSuccess < 0
	?'ERROR - sqltables()failed'
	return
ENDIF

CREATE CURSOR cSQLCommands ( fktable_name C(64), sqlcommand C(254) )

SELECT cTableList
SCAN
	lcSQL = 'exec sp_fkeys ' + ALLTRIM(cTableList.table_name)
	IF USED('cFKList')
		USE IN cFKList
	ENDIF
	IF sqlexec(lnDSN, lcSQL, 'cFKList') > 0
		SELECT cFKList
		SCAN
			lcSQLCommand = 'alter table ' + ALLTRIM(cFKList.fktable_name) + ;
						   ' add constraint ' + ALLTRIM(cFKList.fk_name) + ;
						   ' foreign key (' + ALLTRIM(cFKList.fkcolumn_name) + ') references ' + ;
						   ALLTRIM(cFKList.pktable_name) + ' (' + ALLTRIM(cFKList.pkcolumn_name) + ')'
			INSERT INTO cSQLCommands (fktable_name, sqlcommand) VALUES(cFKList.fktable_name, lcSQLCommand)
		ENDSCAN
	ENDIF
ENDSCAN

?sqldisco(0)

SELECT cSQLCommands
INDEX ON fktable_name TAG fktl
SET ORDER TO fktl

lcString = ''
SCAN
	lcString = lcString + CHR(13)+CHR(10) + ALLTRIM(sqlcommand)
ENDSCAN

STORE lcString TO _cliptext
?lcString

* CLOSE DATA ALL


>Hello,
>
>SQL Server 6.5.
>I need to drop all the constraints, load some data, then re-create the constraints. I was thinking that I should be able to query the system tables to generate the SQL for re-creating the constraints.
>
>Can this be done?
>
>If so, then I have gotten as far as building a list of table_name, contraint_name, referencing_table, referenced_table by joining sysobjects with sysreferences. I cannot figure out what to join to get the referenced columns. Here is what I have so far ...
>
>select so.name as table_name,
>	sofkc.name as fk_constraint,
>	sr.constid, sofkc.id,
>	so2.name as referencing_table_name,
>	so3.name as refered_table_name
>from 	sysobjects so, sysobjects sofkc, sysreferences sr,
>	sysobjects so2, sysobjects so3
>where so.name = 'shopper'
>and so.type = 'U'
>and sofkc.type = 'F'
>and sr.constid = sofkc.id
>and sr.fkeyid = so2.id
>and sr.rkeyid = so3.id
>order by table_name, fk_constraint
>
>
>I have included a table name in the where clause (so.name = 'shopper') to make it easier to determine if I'm getting what I want. However, once I have it working, I would like to get all the tables.
>
>Any suggestions would be greatly appreciated.
>
>TIA,
>-Isaac
Previous
Reply
Map
View

Click here to load this message in the networking platform