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

Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 1008
Write below query:-
select tablename, 
left(col,len(col)-1) as column_name
select distinct tab.table_name tablename,
select col.column_name +',' as [text()]
from information_schema.constraint_column_usage col
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
where t.col is not null
order by tablename;

Comments or Responses

Login to post response