Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent query in sql server for INFORMATION_SCHEMA

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 ?

like image 961
sana Avatar asked Apr 30 '12 15:04

sana


People also ask

What is INFORMATION_SCHEMA in SQL?

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.

Where is INFORMATION_SCHEMA SQL Server?

The INFORMATION_SCHEMA views allow you to retrieve metadata about the objects within a database. These views can be found in the master database under Views / System Views and be called from any database in your SQL Server instance.

Is INFORMATION_SCHEMA deprecated?

The INFORMATION_SCHEMA is a deprecated schema that I believe Microsoft keeps around because it's a notion of ANSI SQL (SQL-92) compliance. As the warning you quoted mentions, the INFORMATION_SCHEMA may be missing meta-data and information on some of the objects in your database.

What schema is INFORMATION_SCHEMA in?

The information schema views are defined in a special schema named INFORMATION_SCHEMA. This schema is contained in each database. Each information schema view contains metadata for all data objects stored in that particular database.


1 Answers

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:

  • The case against INFORMATION_SCHEMA views

You want to use sys.indexes and sys.index_columns for this. For example:

DECLARE @tablename NVARCHAR(512) = 'dbo.tbl_ClientDN';

SELECT
    [Index]  = i.name,
    [Column] = c.Name, 
    [Type]   = i.type_desc,
    PK = i.is_primary_key,
    [Unique] = i.is_unique,
    [Unique Constraint] = i.is_unique_constraint,
    [DESC] = ic.is_descending_key,
    [INCLUDE] = ic.is_included_column,
    [Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
    sys.indexes AS i 
INNER JOIN 
    sys.index_columns AS ic 
    ON i.[object_id] = ic.[object_id] 
    AND i.index_id = ic.index_id
INNER JOIN 
    sys.columns c
    ON ic.column_id = c.column_id
    AND ic.[object_id] = c.[object_id]
WHERE 
    i.[object_id] = OBJECT_ID(@tablename)
ORDER BY [Index], ic.index_column_id;

If you want to do this for all tables at once, then simple changes:

SELECT
    [Table] = QUOTENAME(OBJECT_SCHEMA_NAME(i.[object_id]))
      + '.' + QUOTENAME(OBJECT_NAME(i.[object_id])),
    [Index]  = i.name,
    [Column] = c.Name, 
    [Type]   = i.type_desc,
    PK = i.is_primary_key,
    [Unique] = i.is_unique,
    [Unique Constraint] = i.is_unique_constraint,
    [DESC] = ic.is_descending_key,
    [INCLUDE] = ic.is_included_column,
    [Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
    sys.indexes AS i 
INNER JOIN 
    sys.index_columns AS ic 
    ON i.[object_id] = ic.[object_id] 
    AND i.index_id = ic.index_id
INNER JOIN 
    sys.columns c
    ON ic.column_id = c.column_id
    AND ic.[object_id] = c.[object_id]
ORDER BY [Table], [Index], ic.index_column_id;

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:

  • The cost of reinventing the wheel
like image 166
Aaron Bertrand Avatar answered Oct 05 '22 22:10

Aaron Bertrand