2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQL Serverのビューの列情報を取得する

Last updated at Posted at 2015-02-27

参考
https://msdn.microsoft.com/ja-jp/library/ms186816.aspx

decimal付近の型サイズを取得する為にxprecなど使いましたがサポートはされていない項目とのこと。

SELECT    o.name AS TableName --テーブル名
    ,
    c.name AS ColumnName --列名
    ,
    t.name AS ColumnTypeName --データ型
    ,
    case
        when t.name = 'nvarchar' then convert(varchar, c.length / 2)
        when t.name = 'nchar' then convert(varchar, c.length / 2)
        when t.name = 'datetime' then '-'
        when t.name = 'bigint' then '-'
        when t.name = 'money' then '-'
        when t.name = 'real' then '-'
        when t.name = 'int' then '-'
        when t.name = 'smallint' then '-'
        when t.name = 'decimal' then convert(varchar, c.xprec) + '.' + convert(varchar, c.xscale)
        when t.name = 'numeric' then convert(varchar, c.length * 2)
        else convert(varchar, c.length)
    end AS ColumnLength --データの長さ
    ,
    case
        when c.isnullable = 1 then 'null'
        else 'null以外'
    end AS ColumnNullOK --null許可=1
FROM
    syscolumns as c
    INNER JOIN
        (
            SELECT
                id,
                xtype,
                name
            FROM
                sysobjects
            WHERE
                xtype IN('U', 'V')
            and sysobjects.name = 'view_interface_purchase_item_sales_item'
        ) AS o
    ON  c.id = o.id
    INNER JOIN
        (
            SELECT
                xtype,
                xusertype,
                type,
                usertype,
                name
            FROM
                systypes
        ) AS t
    ON  c.xtype = t.xtype
    AND c.xusertype = t.xusertype
ORDER BY
    colorder

フォーマットツール
http://www-atl.blog.so-net.ne.jp/

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?