sql语句添加表,字段的描述
--添加表的描述 --格式如右:execute sp_addextendedproperty ‘MS_Description‘,‘字段备注信息‘,‘user‘,‘dbo‘,‘table‘,‘字段所属的表名‘,‘column‘,‘添加注释的字段名‘; --添加表描述 EXECUTE sp_addextendedproperty ‘C‘, ‘课程表‘, ‘user‘, ‘dbo‘, ‘table‘, ‘Course‘,NULL, NULL; --添加字段描述 EXECUTE sp_addextendedproperty ‘MS_Description‘, ‘课程ID‘, ‘user‘, ‘dbo‘,‘table‘, ‘Course‘, ‘column‘, ‘CourseId‘;
或者:右键表(列)的属性,找到扩展属性
①获取表名
--查询所有的表名称 SELECT name FROM sysobjects WHERE xtype = ‘u‘; -- xtype:类型可以区分表,视图,存储过程,函数等等
②获取表的描述 sys.extended_properties 这张表存的扩展描述
--查询表的描述 SELECT tbs.name 表名 , ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sysobjects tbs ON ds.major_id = tbs.id WHERE ds.minor_id = 0 AND tbs.name = ‘Course‘;
最后整理:
--表和描述 SELECT sb.name, se.value FROM sysobjects sb LEFT JOIN [sys].[extended_properties] se ON sb.id = se.major_id AND se.minor_id = 0 WHERE sb.xtype = ‘U‘;
③获取表的主键信息
--获取主键字段 sysindexkeys SELECT * FROM SysColumns WHERE id=Object_Id(‘Course‘) and colid=(select TOP 1 keyno from sysindexkeys WHERE id=Object_Id(‘Course‘)); --表 sys.indexes SELECT * FROM sys.indexes WHERE is_primary_key = 1 AND object_id =Object_Id(‘Course‘)
--最后整理 SELECT sb.name, CAST(CASE WHEN ( SELECT COUNT(1) FROM sysindexkeys sk WHERE sk.id = sb.id ) >= 1 THEN 1 ELSE 0 END AS BIT) AS HasPrimaryKey FROM sysobjects sb WHERE sb.type = ‘U‘;
④获取主键的类型和名称
第3步的上面进行扩展
SELECT sc.name [Name],st.name [Type] FROM SysColumns sc LEFT JOIN systypes st ON sc.xtype = st.xtype WHERE id=Object_Id(‘Course‘) AND colid=(select TOP 1 keyno from sysindexkeys WHERE id=Object_Id(‘Course‘));
--整理 SELECT sb.name, sc.name [ColumnName], st.name [ColumnType] FROM sysobjects sb LEFT JOIN syscolumns sc ON sc.id = sb.id AND sc.colid = ( SELECT TOP 1 keyno FROM sysindexkeys WHERE id = sb.id ) LEFT JOIN systypes st ON sc.xtype = st.xtype WHERE sb.type = ‘U‘;
或者:
----查询该表中的所有列的类型和名称以及id SELECT b.name AS [Type], a.name AS [Name], a.colid FROM syscolumns a INNER JOIN systypes b ON a.xtype = b.xtype WHERE id = OBJECT_ID(‘Course‘) AND b.name!=‘sysname‘;
--得到表的主键列id SELECT sc.column_id FROM sys.indexes si INNER JOIN sys.index_columns sc ON si.index_id = sc.index_id AND si.object_id = sc.object_id WHERE si.is_primary_key = 1 AND si.object_id = OBJECT_ID(‘Course‘);
--整理 SELECT sb.name, t.Name, t.Type FROM sysobjects sb OUTER APPLY ( SELECT b.name AS [Type], a.name AS [Name] FROM syscolumns a INNER JOIN systypes b ON a.xtype = b.xtype WHERE id = sb.id AND a.colid IN ( --得到主键id SELECT sc.column_id FROM sys.indexes si INNER JOIN sys.index_columns sc ON si.index_id = sc.index_id AND si.object_id = sc.object_id WHERE si.is_primary_key = 1 AND si.object_id = sb.id ) ) t WHERE sb.type = ‘U‘;
⑤最终结果:
--查询表名,行数,表注释,主键名,主键类型,是否有主键 可以根据自己要的进行更改 SELECT sb.name Tablename, idx.rows, se.value TableDesc, sc.name TablePrimarkeyName, st.name TablePrimarkeyType, CAST(CASE WHEN ( SELECT COUNT(1) FROM sysindexkeys sk WHERE sk.id = sb.id ) >= 1 THEN 1 ELSE 0 END AS BIT) AS HasPrimaryKey FROM sysobjects sb LEFT JOIN sys.extended_properties se ON sb.id = se.major_id AND se.minor_id = 0 LEFT JOIN syscolumns sc ON sc.id = sb.id AND sc.colid = ( SELECT TOP 1 keyno FROM sysindexkeys WHERE id = sb.id ) LEFT JOIN systypes st ON sc.xtype = st.xtype INNER JOIN dbo.sysindexes idx ON sb.id = idx.id AND idx.indid <= 1 WHERE sb.xtype = ‘U‘;
原文地址:https://www.cnblogs.com/Sea1ee/p/10306786.html
时间: 2024-11-08 03:02:03