获取sql server数据库表结构

if exists (select 1 from sysobjects where name = 'sysproperties'and xtype = 'V')
begin
    DROP VIEW sysproperties
end
    GO
    CREATE VIEW sysproperties
AS
 SELECT class AS id,Minor_id AS sMallid,* from sys.extended_properties 
 

-------在sql server 2005中获取表结构请先执行以上语句


SELECT
  表名   = CASE a.colorder WHEN 1 THEN c.name ELSE '' END,
  序     = a.colorder,
  字段名 = a.name,
  标识   = CASE COLUMNPROPERTY(a.id,a.name,'IsIdentity') WHEN 1 THEN '√' ELSE '' END,
  主键   = CASE
    WHEN EXISTS (
      SELECT *
      FROM sysobjects
      WHERE xtype='PK' AND name IN (
        SELECT name
        FROM sysindexes
        WHERE id=a.id AND indid IN (
          SELECT indid
          FROM sysindexkeys
          WHERE id=a.id AND colid IN (
            SELECT colid
            FROM syscolumns
            WHERE id=a.id AND name=a.name
          )
        )
      )
    )
    THEN '√'
    ELSE ''
  END,
  类型   = b.name,
  字节数 = a.length,
  长度   = COLUMNPROPERTY(a.id,a.name,'Precision'),
  小数   = CASE ISNULL(COLUMNPROPERTY(a.id,a.name,'Scale'),0)
    WHEN 0 THEN ''
    ELSE CAST(COLUMNPROPERTY(a.id,a.name,'Scale') AS VARCHAR)
  END,
  允许空 = CASE a.isnullable WHEN 1 THEN '√' ELSE '' END,
  默认值 = ISNULL(d.[text],''),
  说明   = ISNULL(e.[value],'')
FROM syscolumns a
  LEFT  JOIN systypes      b ON a.xtype=b.xusertype
  INNER JOIN sysobjects    c ON a.id=c.id AND c.xtype='U' AND c.name<>'dtproperties' and c.name='此处改为你获取的表名称'
  LEFT  JOIN syscomments   d ON a.cdefault=d.id
  LEFT  JOIN sysproperties e ON a.id=e.id AND a.colid=e.smallid
ORDER BY c.name, a.colorder

 

转载于:https://www.cnblogs.com/PingleDay/p/4229368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值