Vikas Vaidya
Vikas Vaidya

Reputation: 368

SQL: List of tables having primary key as DateTime

How can I get list of tables having primary key of DataType 'DateTime'?

I couldn't find any proper way to do it.

Upvotes: 0

Views: 64

Answers (1)

Akash KC
Akash KC

Reputation: 16310

Based on the this query, I've updated query to meet your requirement :

select 
    t.name as TableName,
    tc.name as ColumnName
   from 
    sys.schemas s 
    inner join sys.tables t   on s.schema_id=t.schema_id
    inner join sys.indexes i  on t.object_id=i.object_id
    inner join sys.index_columns ic on i.object_id=ic.object_id 
                                   and i.index_id=ic.index_id
    inner join sys.columns tc on ic.object_id=tc.object_id 
                             and ic.column_id=tc.column_id
    inner join sys.types ty on tc.system_type_id = ty.system_type_id
where i.is_primary_key=1  and ty.name = 'datetime'

Upvotes: 1

Related Questions