(SQL 2005 ):
SELECT ta.name TableName, ind.name IndexName
from sys.tables ta
left outer join sys.indexes ind
on ind.object_id = ta.object_id
and ind.is_primary_key = 1
IndexName Null.
. () ?
- ----------------
, - . :
SELECT ta.name TableName, ind.name IndexName, indcol.key_ordinal IndexColumnOrder, col.name ColumnName
from sys.tables ta
left outer join sys.indexes ind
on ind.object_id = ta.object_id
and ind.is_primary_key = 1
left outer join sys.index_columns indcol
on indcol.object_id = ta.object_id
and indcol.index_id = ind.index_id
left outer join sys.columns col
on col.object_id = ta.object_id
and col.column_id = indcol.column_id
order by
ta.Name
,indcol.key_ordinal
( ) :
SELECT ta.name TableName, max(col.name) ColumnName
from sys.tables ta
inner join sys.indexes ind
on ind.object_id = ta.object_id
and ind.is_primary_key = 1
inner join sys.index_columns indcol
on indcol.object_id = ta.object_id
and indcol.index_id = ind.index_id
inner join sys.columns col
on col.object_id = ta.object_id
and col.column_id = indcol.column_id
group by ta.name
having count(*) = 1
order by ta.Name
As for converting this code to C # (not my file), you can either make it a stored procedure, or just create and send a request from your code.
source
share