zoukankan      html  css  js  c++  java
  • 获取SQL SERVER 2000/2005数据库字段属性(转)

    --SQL SERVER 2000 和 2005结构是不一样的,所以在获取SQL SERVER 2000/2005字段属性时需要分别来获取。

    --1。SQL SERVER 2000

    SELECT
         字段名     = a.name,
         主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
                          SELECT name FROM sysindexes WHERE indid in(
                             SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '1' else '0' end,
         标识       = CASE WHEN COLUMNPROPERTY(a.id, a.name,
                           'IsIdentity') = 1 THEN '1' ELSE '0' END,
         类型       = b.name,
        占用字节数 = a.length,
        长度       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
        小数       =  a.xscale,
        可空       = a.isnullable,
        默认值     = 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='UserAccount'

    --d.name = 'UserAccount',UserAccount为你需要查找的数据表。


    --2。SQL SERVER 2005


    SELECT     CASE WHEN EXISTS
                               (SELECT     1
                                 FROM          sysobjects
                                 WHERE      xtype = 'PK' AND parent_obj = a.id AND name IN
                                                           (SELECT     name
                                                              FROM          sysindexes
                                                              WHERE      indid IN
                                                                                         (SELECT     indid
                                                                                           FROM          sysindexkeys
                                                                                          WHERE      id = a.id AND colid = a.colid))) THEN '1' ELSE '0' END AS 'key', CASE WHEN COLUMNPROPERTY(a.id, a.name,
                          'IsIdentity') = 1 THEN '1' ELSE '0' END AS 'identity', a.name AS ColName, c.name AS TypeName, a.length AS 'byte', COLUMNPROPERTY(a.id, a.name,
                          'PRECISION') AS 'length', a.xscale, a.isnullable, ISNULL(e.text, '') AS 'default', ISNULL(p.value, '') AS 'comment'
    FROM         sys.syscolumns AS a INNER JOIN
                          sys.sysobjects AS b ON a.id = b.id INNER JOIN
                          sys.systypes AS c ON a.xtype = c.xtype LEFT OUTER JOIN
                          sys.syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN
                          sys.extended_properties AS p ON a.id = p.major_id AND a.colid = p.minor_id
    WHERE     (b.name = 'keyfactory') AND (c.status <> '1')
    --b.name = 'Keyfactory','Keyfactory'为你想要查找的数据表。

  • 相关阅读:
    flask 使用 SQLAlchemy 的两种方式
    python package 的两种组织方式
    sqlalchemy 的 raw sql 方式使用示例
    Git高速入门——Git安装、创建版本号库以及经常使用命令
    mariadb 10.1.10安装
    Android studio中导入第三方类库
    怎样获取oracle dbid
    宇宙中为何存在“黑洞”?
    CSDN日报20170401 ——《假设你还是“程序猿”,我劝你别创业!》
    clang-format中文出错
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1421299.html
Copyright © 2011-2022 走看看