Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291769
  • 博文数量: 163
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -40
  • 用 户 组: 普通用户
  • 注册时间: 2017-03-08 00:28
文章分类

全部博文(163)

文章存档

2015年(2)

2014年(35)

2013年(28)

2012年(30)

2011年(22)

2010年(14)

2009年(8)

2008年(13)

2007年(11)

分类: 数据库开发技术

2012-09-21 14:21:07

SQL server query to get the list of columns in a table along with data types, not null, and primary key constraints

I need to write a query on SQL server to get the list of columns in a particular table, its associated data types and their length and if they are not null. I have managed to do this much. But now i also need to get in the same table against a column - TRUE if it is a primary key. How do i do this ?

This is how the output should be:

Columns_name----Data type----Length----isnull----Pk

SELECT
    c
.name 'Column Name',
    t
.Name 'Data type',
    c
.max_length 'Max Length',
    c
.precision ,
    c
.scale ,
    c
.is_nullable,
    ISNULL
(i.is_primary_key, 0) 'Primary Key'
FROM    
    sys
.columns c
INNER JOIN
    sys
.types t ON c.system_type_id = t.system_type_id
LEFT OUTER JOIN
    sys
.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
    sys
.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c
.object_id = OBJECT_ID('YourTableName')

How about this:

SELECT
    c
.name 'Column Name',
    t
.Name 'Data type',
    c
.max_length 'Max Length',
    c
.precision ,
    c
.scale ,
    c
.is_nullable,
    ISNULL
(i.is_primary_key, 0) 'Primary Key'
FROM    
    sys
.columns c
INNER JOIN
    sys
.types t ON c.system_type_id = t.system_type_id
LEFT OUTER JOIN
    sys
.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
    sys
.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c
.object_id = OBJECT_ID('YourTableName')

Just replace YourTableName with your actual table name - works for SQL Server 2005 and up.

you can do this to get the PK constraint

Select C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH, C.NUMERIC_PRECISION, C.IS_NULLABLE, TC.CONSTRAINT_NAME
From INFORMATION_SCHEMA.COLUMNS As C
   
Left Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
     
On TC.TABLE_SCHEMA = C.TABLE_SCHEMA
         
And TC.TABLE_NAME = C.TABLE_NAME
         
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
Where C.TABLE_NAME = 'Table'


I must have missed that you want a flag to determine if the given column was part of the PK instead of the name of the PK constraint. For that you would use:

Select C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH
   
, C.NUMERIC_PRECISION, C.NUMERIC_SCALE
   
, C.IS_NULLABLE
   
, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
From INFORMATION_SCHEMA.COLUMNS As C
   
Outer Apply (
               
Select CCU.CONSTRAINT_NAME
               
From INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
                   
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
                       
On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
               
Where TC.TABLE_SCHEMA = C.TABLE_SCHEMA
                   
And TC.TABLE_NAME = C.TABLE_NAME
                   
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                   
And CCU.COLUMN_NAME = C.COLUMN_NAME
               
) As Z
Where C.TABLE_NAME = 'Table'

select DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION
, DATETIME_PRECISION,
       IS_NULLABLE
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='TableName'

to get all the metadata you require except for the Pk information.

The stored procedure sp_columns returns detailed table information.

exec sp_columns MyTable

原文地址:


阅读(1172) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~