我想获取表的所有列与确定字段的额外列是否为主键。我使用此代码:
SELECT c.column_id ID, c.name ItemName,
CASE
WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
ELSE 0
END ISPK
FROM sys.tables tb
JOIN sys.columns c
ON tb.name = 'register'
AND tb.[object_id] = c.[object_id]
JOIN sys.types tp
ON c.user_type_id = tp.user_type_id
LEFT JOIN sys.computed_columns cc
ON c.[object_id] = cc.[object_id]
AND c.column_id = cc.column_id
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
ON c.name = k.COLUMN_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tblCon
ON tblCon.TABLE_NAME = 'register'
AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME
在此示例中,tablename为“register”。
此代码是正确的,但对于主键字段,返回两条记录:1
有什么问题?
答案 0 :(得分:0)
这应该有效
SELECT c.column_id ID, c.name ItemName,
CASE
WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
ELSE 0
END ISPK
FROM sys.tables tb
JOIN sys.columns c
ON tb.name = 'register'
AND tb.[object_id] = c.[object_id]
JOIN sys.types tp
ON c.user_type_id = tp.user_type_id
LEFT JOIN sys.computed_columns cc
ON c.[object_id] = cc.[object_id]
AND c.column_id = cc.column_id
LEFT JOIN (select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_NAME like 'PK_%' and TABLE_NAME='register') AS K
ON c.name = k.COLUMN_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tblCon
ON tblCon.TABLE_NAME = 'register'
AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME
答案 1 :(得分:0)
从Sagar那里得到答案并大大清理它。有两个额外的连接,我将子查询更改为左连接并修改了一些连接条件,因此表名只需要输入一次。
SELECT c.column_id AS ID
, c.name AS ItemName
, CASE
WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
ELSE 0
END AS ISPK
FROM sys.tables tb
JOIN sys.columns c ON
tb.object_id = c.object_id
AND tb.[object_id] = c.[object_id]
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K on
CONSTRAINT_NAME like 'PK_%'
and TABLE_NAME = tb.name
AND c.name = k.COLUMN_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tblCon ON
tblCon.TABLE_NAME = tb.name
AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME
where tb.name = 'register'
答案 2 :(得分:0)
添加了一个必填列作为详细信息。
SELECT c.column_id ID, c.name ItemName,
CASE
WHEN tblCon.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
ELSE 0
END ISPK,
ISNULL(tblCon.CONSTRAINT_TYPE,'') as Detail
FROM sys.tables tb
JOIN sys.columns c
ON tb.name = 'register'
AND tb.[object_id] = c.[object_id]
JOIN sys.types tp
ON c.user_type_id = tp.user_type_id
LEFT JOIN sys.computed_columns cc
ON c.[object_id] = cc.[object_id]
AND c.column_id = cc.column_id
LEFT JOIN (select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where (CONSTRAINT_NAME like 'PK_%' or CONSTRAINT_NAME like 'FK_%') and TABLE_NAME='register') AS K
ON c.name = k.COLUMN_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tblCon
ON tblCon.TABLE_NAME = 'register'
AND tblCon.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND tblCon.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND tblCon.CONSTRAINT_NAME = K.CONSTRAINT_NAME