2009年7月20日 星期一

SQL 取得相關資料表及欄位說明

= SQL Server
SQL Command:
1.取得資料庫表單數量
select count(*) as totaltablenumber
from sysobjects
where xtype = 'U';

2.取得資料表名稱(tablename)及欄位數量(columnnumber)
select name as tablename, info as columnnumber
from sysobjects
where xtype = 'U';

3.取得某一資料表內所有欄位名稱
select b.name
from sysobjects as a, syscolumns as b
where a.xtype = 'U' and a.id = b.id and a.name='資料表單名稱';

3.1 取得某一資料表內所有欄位名稱
EXEC sp_columns 表單名稱

Store Procedure:
資料庫中所有資料表:透過內定的Stored Procedure sp_tables
  1. EXEC sp_tables
  2. @table_name = '%',
  3. @table_owner = 'dbo',
  4. @table_qualifier = @DBName;
取得資料表Schema
sp_columns @TableName

取得欄位說明、備註(Extended Property):這個是透過Sql內定的Function來處理
  1. SELECT *
  2. FROM ::fn_listextendedproperty(NULL, 'user', 'dbo', 'table', @TableName, 'column', default)
取得主索引
sp_pkeys @TableName

沒有留言:

張貼留言