在winform程序开发中,涉及到对某个表的组合查询时会到数据库中提取表的字段名和说明,以便于用户选择查询,在C#中使用语句比较麻烦,写到存储过程中就比较方便了。
USE [schoolDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_showcolumn]
@tablename varchar(30)
as
select c.name as 字段名,p.value as 说明 from sysobjects o left join syscolumns c on o.id=c.id
left join sys.extended_properties p on p.major_id=c.id and p.minor_id=c.colid and p.name='MS_Description' left join systypes t on c.xusertype=t.xusertype
where o.type='u'
and o.name=@tablename
查询更多信息的sql语句:
select o.name as tableName,c.name as columnName,t.name as columnType,p.value as columnDescription from sysobjects o left join syscolumns c on o.id=c.id
left join sys.extended_properties p on p.major_id=c.id and p.minor_id=c.colid and p.name='MS_Description' left join systypes t on c.xusertype=t.xusertype
where o.type='u'
and o.name='cdxmain'