日期:2014-05-18 浏览次数:20488 次
主键=case when exists(SELECT 1 FROM '+@dbname1+'..sysobjects where xtype=''PK'' and name in ( SELECT name FROM '+@dbname1+'..sysindexes WHERE indid in( SELECT indid FROM '+@dbname1+'..sysindexkeys WHERE id = a.id AND colid=a.colid ))) then 1 else 0 end,
如何取主键字段名称及字段类型 --得到主键字段名 1: SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME<>'dtproperties' 2: EXEC sp_pkeys @table_name='表名' 3: select o.name as 表名,c.name as 字段名,k.colid as 字段序号,k.keyno as 索引顺序,t.name as 类型 from sysindexes i join sysindexkeys k on i.id = k.id and i.indid = k.indid join sysobjects o on i.id = o.id join syscolumns c on i.id=c.id and k.colid = c.colid join systypes t on c.xusertype=t.xusertype where o.xtype = 'U' and o.name='要查询的表名' and exists(select 1 from sysobjects where xtype = 'PK' and parent_obj=i.id and name = i.name) order by o.name,k.colid
------解决方案--------------------
zjcxc的这个脚本的确有点问题,这个问题以前有朋友提过。那个帖子的地址是:
http://topic.csdn.net/u/20070625/10/490e3182-9c15-4a42-bf5e-6c99cafda258.html
其中有一处判断不太严谨,修改了一下,请zjcxc指正:
SELECT CASE WHEN a.colorder = 1 THEN d.name ELSE '' END AS 表名, CASE WHEN a.colorder = 1 THEN isnull(CONVERT(nvarchar, f.value), '') ELSE '' END AS 表说明, a.colorder AS 字段序号, a.name AS 字段名, CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识, CASE WHEN EXISTS (SELECT 1 FROM sysobjects WHERE [color=#FF0000]parent_obj = object_id('MyTab') and /*!!!只增加此行!!!*/[/color] xtype = 'PK' AND name IN (SELECT name FROM sysindexes WHERE indid IN (SELECT indid FROM sysindexkeys WHERE id = a.id AND colid = a.colid))) THEN '√' ELSE '' END AS 主键, b.name AS 类型, a.length AS 占用字节数, ......
------解决方案--------------------
确实有楼上的问题
WHERE parent_obj = object_id(d .name) AND xtype = 'PK' AND name IN