zoukankan      html  css  js  c++  java
  • sql查询表结构,过程,视图,主键,外键,约束


     


    1、表结构查询

    SELECT TOP (100) PERCENT a.name AS zdm,COLUMNPROPERTY(a.id, a.name, 'IsIdentity') AS bs ,
    CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
     AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid
     INNER JOIN dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK' WHERE sc.id = a.id AND sc.colid = a.colid)
     THEN '1' ELSE '0' END AS zj , b.name AS lx, a.length AS cd, COLUMNPROPERTY(a.id, a.name,'PRECISION')
     AS jd, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS xsws,a.isnullable AS yxk, ISNULL(e.text, '')
      AS mrz, ISNULL(g.value, '') AS zdsm FROM dbo.syscolumns AS a LEFT OUTER JOIN dbo.systypes AS b ON a.xtype = b.xusertype
      INNER JOIN dbo.sysobjects AS d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT OUTER JOIN
       dbo.syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN sys.extended_properties AS g
       ON a.id = g.major_id AND a.colid = g.minor_id LEFT OUTER JOIN sys.extended_properties
       AS f ON d.id = f.major_id AND f.minor_id = 0  where d .name='查询的表名'

    2、查询存储过程

    select CASE a.xtype  WHEN 'p' THEN '存储过程' end as lx ,a.name, b.text from sysobjects a left outer join syscomments b on a.id = b.id where xtype='p'
    3、查询视图

    select  CASE a.xtype  WHEN 'v' THEN '视图' end as lx,a.name , b.text from sysobjects a left outer join syscomments b on a.id = b.id where xtype='v'
    4、查询主键、外键、约束

    select
    CASE a.xtype  WHEN 'PK' THEN '主键' WHEN 'F' THEN '外键' WHEN 'C' THEN '约束'
    END AS lx,a.name AS name,
    b.text from sysobjects a left outer join syscomments b on a.id = b.id
    where (a.xtype IN ( 'C',  'F','PK')) AND
    (OBJECTPROPERTY(a.id, N'IsMSShipped') = 0) and a.parent_obj=(select id from sysobjects where name = 'table_2')
    以上应用环境是sqlserver2008

  • 相关阅读:
    图片验证码, 登录, 注销, 修改密码
    注册页面及注册功能实现
    高级配置文件, csrf, django settings源码, django auth模块, 文件配置的插拔式设计
    cookie操作, session操作, django中间件
    半自动创建多对多关系表, forms组件
    sweetalert, bulk_create, 分页器
    orm查询优化, MVC与MTV, choices参数, ajax
    聚合查询, 分组查询, F与Q查询, 常见字段及参数, 自定义Char字段, 事务操作
    Contest2058
    ACM版《孔乙己》
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2990602.html
Copyright © 2011-2022 走看看