如何判断一个表的主键是否存在?如何取主键字段名称及字段类型,写SQL语句 急急!!在线
如何判断一个表的主键是否存在?如何取主键字段名称及字段类型,写SQL语句 --------------------编程问答-------------------- 替换your tabe name为你的表名即可判断是否存在主键
select count(*) from [sysobjects] where xtype = 'pk'and parent_obj in (select [id] from [sysobjects] where [name] = 'your table')
为0则没有主键
取主键字段名称和类型
select [name],
CASE xtype
WHEN 34 THEN 'Image'
WHEN 35 THEN 'Text'
WHEN 48 THEN 'TinyInt'
WHEN 52 THEN 'SmallInt'
WHEN 56 THEN 'Int'
WHEN 58 THEN 'SmallDateTime'
WHEN 59 THEN 'Real'
WHEN 60 THEN 'Money'
WHEN 61 THEN 'DateTime'
WHEN 62 THEN 'Float'
WHEN 99 THEN 'NText'
WHEN 104 THEN 'Bit'
WHEN 106 THEN 'Decimal'
WHEN 122 THEN 'SmallMoney'
WHEN 127 THEN 'BigInt'
WHEN 165 THEN 'VarBinary'
WHEN 167 THEN 'VarChar'
WHEN 173 THEN 'Binary'
WHEN 175 THEN 'Char'
WHEN 231 THEN 'NVarChar'
WHEN 239 THEN 'NChar'
ELSE '!MISSING'
end as type
from syscolumns
where [id] in (select [id] from [sysobjects] where [name] = 'your tabe name') and colstat=1 --------------------编程问答-------------------- 可以参考这个
http://www.codeproject.com/cs/database/CSCodeBuilder.asp --------------------编程问答-------------------- 多谢
补充:.NET技术 , C#