I am working on a page where we need to visually compare the schema of same table across the two database-one in sql server and other in mysql.I have to include indexes as well.
now mysql query shows info along with indexes –
select column_name,column_type,table_name,column_key
from INFORMATION_SCHEMA.COLUMNS where table_name = 'tbl_ClientDN'
But for sql server the same query does not return indexes-
select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'tbl_ClientDN'
so i need query to compbine the result of –
sp_helpindex 'tbl_ClientDN'
how to get column_key showing indexes in mssql query.
any suggestion ?
Stay away from
INFORMATION_SCHEMA.COLUMNS, especially for indexes, since things like filtered indexes and included columns are not part of the definition. I talk about this in more detail here:You want to use
sys.indexesandsys.index_columnsfor this. For example:If you want to do this for all tables at once, then simple changes:
More information available in the topics sys.indexes and sys.index_columns.
You also might want to take a look at Kimberley L. Tripp’s sp_helpindex2.
EDIT
In general I agree with @BrianWhite’s comment. If you are spending any effort on this at all, you should be using a tool for this instead of re-inventing the wheel and trying to write it yourself. Troubleshooting this one query you’ve probably already spent, in terms of time, the cost of a good tool. Please read this post: