SELECT
表名=case when a.colorder=1 then d.name else '' end,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where 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 'true' else 'false' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then 'true'else 'false' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xusertype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id and a.colid=g.smallid
left join sysproperties f on d.id=f.id and f.smallid=0
where d.name='BUG' --如果只查询指定表,加上此条件
order by a.id,a.colorder
CREATE PROCEDURE DBO.GETUSERCOLUMNS @TABLEID INTEGER AS
SELECT NAME,ID,XTYPE,TYPESTAT,XUSERTYPE,LENGTH,COLID,XPREC,XSCALE,COLSTAT,ISOUTPARAM FROM SYSCOLUMNS
WHERE ID = @TABLEID
/// <summary>
/// 取对应SQL定义的数据类型
/// </summary>
/// <param name="byType">数据类型id</param>
/// <returns>数据类型</returns>
public static SqlDbType GetSqlType(byte byType)
{
SqlDbType type = SqlDbType.Variant;
switch (byType)
{
case 34://image
type = SqlDbType.Image;
break;
case 35://Text
type = SqlDbType.Text;
break;
case 36://UniqueIdentifier
type = SqlDbType.UniqueIdentifier;
break;
case 48:///byte
type = SqlDbType.TinyInt;
break;
case 52://smallint
type = SqlDbType.SmallInt;
break;
case 55://decimal
type = SqlDbType.Decimal;
break;
case 56://int
type = SqlDbType.Int;
break;
case 58:
type = SqlDbType.SmallDateTime;
break;
case 59:
type = SqlDbType.Real;
break;
case 60://decimal
type = SqlDbType.Money;
break;
case 61://datetime
type = SqlDbType.DateTime;
break;
case 62://float
type = SqlDbType.Float;
break;
case 98://smallint
type = SqlDbType.Variant;
break;
case 99:
type = SqlDbType.NText;
break;
case 104://bit
type = SqlDbType.Bit;
break;
case 106://decimal
type = SqlDbType.Decimal;
break;
case 108://numeric
type = SqlDbType.Decimal;
break;
case 122://smallmoney
type = SqlDbType.SmallMoney;
break;
case 127://bigint
type = SqlDbType.BigInt;
break;
case 165://varbinery
type = SqlDbType.VarBinary;
break;
case 167://varchar
type = SqlDbType.VarChar;
break;
case 173://binary
type = SqlDbType.Binary;
break;
case 175://char
type = SqlDbType.Char;
break;
case 189://timestamp
type = SqlDbType.Timestamp;
break;
case 231:
type = SqlDbType.NVarChar;
break;
case 239:
type = SqlDbType.NChar;
break;
}
return type;
}