Getting Primary key as well as composite Keys and Concatenate both keys into a single comma-separated Row.

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 1657
Write below query:-
select tablename, 
left(col,len(col)-1) as column_name
from
(
select distinct tab.table_name tablename,
(
select col.column_name +',' as [text()]
from information_schema.constraint_column_usage col
where
col.constraint_name = tab.constraint_name and col.table_name = tab.table_name and constraint_type = 'primary key'
for xml path ('')
) col
from information_schema.table_constraints tab
)t
where t.col is not null
order by tablename;

Comments or Responses

Login to post response