On Mon, 29 Jun 2009 11:16:26 GMT,
v-(E-Mail Removed)
(Lingzhi Sun [MSFT]) wrote:
>Hi David,
>
>Data tables with same name but different schemas are supported in SQL
>Server. For detail, please see my post in another your thread: [Is it
>impossible to have schema1.dave and schema2.dave as tables in the same
>DB?].
>
>To retrieve all the FK, PK and other constraints information related to one
>data table, we can use this T-SQL command:
>===============================================================
>exec sp_helpconstraint 'dbo.Products'
>===============================================================
>
>The schema name and the table name can be set in this query. By default,
>the schema name will be ˇ°dboˇ±. The query result contains three result
>sets. The first one is the table object name. The second one consists of
>the detailed information about the FK, PK and other constraints
>information, including constraint type, constraint name, delete update
>actions, status information and constraint key columns. The third set
>contains all the FK related tables for the current table. DataReader can
>be used to retrieve the data records in each result set.
>...
Hi;
I've found MARS to be problematic between it neededing to be
configured just right, etc. I have selects that work except that they
do not return the schema of the PK in the FK:PK relationship. I've
tried to figure out what else I need to do an inner join on but have
had no luck.
What I use for ver 9 (Sql Server 2005) & later is:
cmd.CommandText = "SELECT fc.name AS foreign_column_1,
fc2.name AS foreign_column_2, p.name AS primary_table, " +
"rc.name AS primary_column_1, rc2.name AS
primary_column_2 FROM sys.sysobjects AS f INNER JOIN " +
"sys.sysobjects AS c ON f.parent_obj = c.id INNER
JOIN sys.sysreferences AS r ON f.id = r.constid INNER JOIN " +
"sys.sysobjects AS p ON r.rkeyid = p.id INNER JOIN
sys.syscolumns AS rc ON r.rkeyid = rc.id AND " +
"r.rkey1 = rc.colid INNER JOIN sys.syscolumns AS
fc ON r.fkeyid = fc.id AND r.fkey1 = fc.colid LEFT OUTER JOIN " +
"sys.syscolumns AS rc2 ON r.rkeyid = rc2.id AND
r.rkey2 = rc.colid LEFT OUTER JOIN sys.syscolumns AS fc2 ON " +
"r.fkeyid = fc2.id AND r.fkey2 = fc.colid WHERE
(f.type = 'F') AND (c.name = '" + sysTable + "')";
And for ver 8:
cmd.CommandText = "SELECT fc.name AS foreign_column_1,
fc2.name AS foreign_column_2, p.name AS primary_table, " +
"rc.name AS primary_column_1, rc2.name AS
primary_column_2 FROM sysobjects AS f INNER JOIN " +
"sysobjects AS c ON f.parent_obj = c.id INNER JOIN
sysreferences AS r ON f.id = r.constid INNER JOIN " +
"sysobjects AS p ON r.rkeyid = p.id INNER JOIN
syscolumns AS rc ON r.rkeyid = rc.id AND " +
"r.rkey1 = rc.colid INNER JOIN syscolumns AS fc ON
r.fkeyid = fc.id AND r.fkey1 = fc.colid LEFT OUTER JOIN " +
"syscolumns AS rc2 ON r.rkeyid = rc2.id AND
r.rkey2 = rc.colid LEFT OUTER JOIN syscolumns AS fc2 ON " +
"r.fkeyid = fc2.id AND r.fkey2 = fc.colid WHERE
(f.type = 'F') AND (c.name = '" + sysTable + "')";
Is there a way to get the schema from these and/or is there a better
select I can do? My code will work fine if I do a single select for
all FK:PK mappings for a table or if I have to do it column by column.
What I need is for a given column (in a given table) that is a foreign
key, I can get the the schema, table, & column of the primary key it
maps to.
At present we are only handling the case of a single PK:FK mapping but
if I can get all back, better to have that info now.
How can I do this?
thanks - dave
david@at-at-(E-Mail Removed)
Windward Reports --
http://www.WindwardReports.com
me --
http://dave.thielen.com
Cubicle Wars -
http://www.windwardreports.com/film.htm