top of page

Find All Primary Key in SQL Server

select schema_name(tab.schema_id) as [schema_name],

pk.[name] as pk_name,

substring(column_names, 1, len(column_names)-1) as [columns],

tab.[name] as table_name

from sys.tables tab with (nolock)

inner join sys.indexes pk with (nolock)

on tab.object_id = pk.object_id

and pk.is_primary_key = 1

cross apply (select col.[name] + ', '

from sys.index_columns ic

inner join sys.columns col

on ic.object_id = col.object_id

and ic.column_id = col.column_id

where ic.object_id = tab.object_id

and ic.index_id = pk.index_id

order by col.column_id

for xml path ('') ) D (column_names)

--Filter by Table Name

--where tab.name like '%ProductionReadinessReview%'

order by schema_name(tab.schema_id),

pk.[name]

6 views0 comments

Recent Posts

See All

SQL Server Profiler Trace

--Import multiple trace file in sql Table. --All the rollover files will be automatically imported --provide number tracefile to capture...

Comments


bottom of page