Gestern fragte jemand in der Newsgroup microsoft.public.de.sqlserver wie man die Primärschlüssel-Felder aus INFORMATION_SCHEMA-Tabellen ermitteln kann.
So geht:
select CU.COLUMN_NAME
from INFORMATION_SCHEMA.KEY_COLUMN_USAGE as CU
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
on CU.CONSTRAINT_CATALOG=TC.CONSTRAINT_CATALOG
and CU.CONSTRAINT_SCHEMA=TC.CONSTRAINT_SCHEMA
and CU.CONSTRAINT_NAME=TC.CONSTRAINT_NAME
where CONSTRAINT_TYPE = N'PRIMARY KEY'
and TC.TABLE_CATALOG = N'AdventureWorks'
and TC.TABLE_SCHEMA = N'Person'
and TC.TABLE_NAME = N'MyAddress'
order by ORDINAL_POSITION asc
Damit bekommt man eine Liste der Felder in der richtigen Reihenfolge.