文章出處

開發中經常用到查詢指定表及其字段的信息,以下是我整理的SQL語句查詢方法,供自己平時使用也提供給大家參考!

1.適用MS SQL SERVER:

 1 SELECT
 2 表名 = case when a.colorder=1 then d.name else '' end,
 3 表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
 4 字段序號 = a.colorder,
 5 字段名 = a.name,
 6 標識 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then ''else '' end,
 7 主鍵 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
 8 SELECT name FROM sysindexes WHERE indid in(
 9 SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '' else '' end,
10 類型 = b.name,
11 占用字節數 = a.length,
12 長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
13 小數位數 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
14 允許空 = case when a.isnullable=1 then ''else '' end,
15 默認值 = isnull(e.text,''),
16 字段說明 = isnull(g.[value],'')
17 FROM
18 syscolumns a
19 left join
20 systypes b
21 on
22 a.xusertype=b.xusertype
23 inner join
24 sysobjects d
25 on
26 a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
27 left join
28 syscomments e
29 on
30 a.cdefault=e.id
31 left join
32 sys.extended_properties g
33 on
34 --a.id=g.id and a.colid=g.smallid
35 a.id=g.major_id and a.colid=g.Minor_id
36 left join
37 sys.extended_properties f
38 on
39 --d.id=f.id and f.smallid=0
40 d.id=f.major_id and f.Minor_id=0
41 where
42 d.name='表名' --如果只查詢指定表,加上此條件
43 order by
44 a.id,a.colorder

2.適用ORACLE:

 1 SELECT
 2 USER_TAB_COLS.TABLE_NAME as 表名,
 3 user_tab_comments.comments as 表備注,
 4 USER_TAB_COLS.COLUMN_ID as 列序號,
 5 user_col_comments.comments as 列備注,
 6 USER_TAB_COLS.COLUMN_NAME as 列名 ,
 7 USER_TAB_COLS.DATA_TYPE as 數據類型,
 8 USER_TAB_COLS.DATA_LENGTH as 長度,
 9 USER_TAB_COLS.NULLABLE as 是否為空,
10 user_cons_columns.constraint_name as 約束名,
11 user_constraints.constraint_type as 主鍵
12 FROM USER_TAB_COLS inner join user_col_comments on
13 user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME
14 and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME
15 INNER join user_cons_columns on user_cons_columns.table_name=USER_TAB_COLS.table_name
16 INNER join user_constraints on user_constraints.table_name=USER_TAB_COLS.table_name and user_constraints.constraint_name=user_cons_columns.constraint_name
17 inner join user_tab_comments on USER_TAB_COLS.TABLE_NAME=user_tab_comments.TABLE_NAME
18 WHERE USER_TAB_COLS.table_name='表名'
19 ORDER BY USER_TAB_COLS.TABLE_NAME

原文其它網址:http://www.zuowenjun.cn/post/2014/08/28/26.html


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()