zoukankan      html  css  js  c++  java
  • Sql Server通过系统信息表获取表中字段信息的方法

    获取表中各字段信息:
    select * from syscolumns where id=object_id('表名')

    获取表中主键的方法:
    select * from sysobjects where xtype ='pk'

    外键同理

    举例:查询表'cdt_1x_basic'的字段信息(包括主键信息)
    select a.*, 
    case b.IsPK when 1 then 1 else 0 end as IsPK
    from 
    (
      select * from syscolumns where id=object_id('dbo.cdt_1x_basic')
    )a left join
    (
      SELECT TABLE_NAME,COLUMN_NAME,1 as IsPK FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE  
      WHERE TABLE_NAME='cdt_1x_basic'    --这里千万不能加前缀dbo.
    )b on a.name=b.COLUMN_NAME

    附1,系统表syscolumns的各字段及信息定义:
    列名    数据类型             描述 

    name sysname 列名或过程参数的名称。 
    id int 该列所属的表对象 ID,或与该参数关联的存储过程 ID。 
    xtype tinyint systypes 中的物理存储类型。 
    typestat tinyint 仅限内部使用。 
    xusertype smallint 扩展的用户定义数据类型 ID。 
    length smallint systypes 中的最大物理存储长度。 
    xprec tinyint 仅限内部使用。 
    xscale tinyint 仅限内部使用。 
    colid smallint 列或参数 ID。 
    xoffset smallint 仅限内部使用。 
    bitpos tinyint 仅限内部使用。 
    reserved tinyint 仅限内部使用。 
    colstat smallint 仅限内部使用。 
    cdefault int 该列的默认值 ID。 
    domain int 该列的规则或 CHECK 约束 ID。 
    number smallint 过程分组时(0 表示非过程项)的子过程号。 
    colorder smallint 仅限内部使用。 
    autoval varbinary(255) 仅限内部使用。 
    offset smallint 该列所在行的偏移量;如果为负,表示可变长度行。 
    status tinyint 用于描述列或参数属性的位图: 

    0x08 = 列允许空值。 
    0x10 = 当添加 varchar 或 varbinary 列时,ANSI 填充生效。保留 varchar 列的尾随空格,保留 varbinary 列的尾随零。 
    0x40 = 参数为 OUTPUT 参数。 
    0x80 = 列为标识列。 

    type tinyint systypes 中的物理存储类型。 
    usertype smallint systypes 中的用户定义数据类型 ID。 
    printfmt varchar(255) 仅限内部使用。 
    prec smallint 该列的精度级别。 
    scale int 该列的小数位数。 
    iscomputed int 表示是否已计算该列的标志: 

    0 = 未计算。 
    1 = 已计算。 

    isoutparam int 表示该过程参数是否是输出参数: 

    1 = 真。 
    0 = 假。 

    isnullable int 表示该列是否允许空值: 

    1 = 真。 
    0 = 假。


    附2,表syscolumns中xtype的数据类型对应说明
    xtype 类型
    34 image
    35 text
    36 uniqueidentifier
    48 tinyint
    52 smallint
    56 int
    58 smalldatetime
    59 real
    60 money
    61 datetime
    62 float
    98 sql_variant
    99 ntext
    104 bit
    106 decimal
    108 numeric
    122 smallmoney
    127 bigint
    165 varbinary
    167 varchar
    173 binary
    175 char
    189 timestamp
    231 sysname
    231 nvarchar
    239 nchar
     
    Grass Mud Horse
  • 相关阅读:
    【译】x86程序员手册30-8.2 I/O指令
    【译】x86程序员手册29-第8章 输入输出
    【译】x86程序员手册28-7.7任务地址空间
    【译】x86程序员手册27-7.6任务链
    【译】x86程序员手册26-7.5任务切换
    【译】x86程序员手册25-7.1任务状态段
    【译】x86程序员手册23-6.5组合页与段保护
    【译】x86程序员手册24-第7章 多任务
    【译】x86程序员手册22-6.4页级保护
    WPF Blend Grid 布局
  • 原文地址:https://www.cnblogs.com/Scissors/p/2754041.html
Copyright © 2011-2022 走看看