程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> 將SQL Server中所有表的列信息顯示出來

將SQL Server中所有表的列信息顯示出來

編輯:關於SqlServer

正在作一個關於SQL SERVER數據庫導入Excel文件的程序,要讀取數據庫中的列的信息,從網上找了很多資料,終於總結出來比較理想的sql語句,執行後返回的列分別是:表名、列名、列類型、列長度、列描述、是否主鍵,語句如下:1 Select Sysobjects.Name As Tb_name, Syscolumns.Name As Col_name, Systypes.Name As Col_type, Syscolumns.Length As Col_len, Isnull(Sysproperties.Value,Syscolumns.Name) As Col_memo,
2 Case When Syscolumns.Name In
3 (Select 主鍵=A.Name
4 From Syscolumns A
5 Inner Join Sysobjects B On A.Id=B.Id And B.Xtype='U' And B.Name<>'Dtproperties'
6 Where Exists(Select 1 From Sysobjects Where Xtype='Pk' And Name In (
7 Select Name From Sysindexes Where Indid In(
8 Select Indid From Sysindexkeys Where Id = A.Id And Colid=A.Colid
9 )))
10 And B.Name=Sysobjects.Name
11 )
12 Then 1 Else 0 End As Is_key
13
14 From Sysobjects,Systypes,Syscolumns
15 Left Join Sysproperties On (Syscolumns.Id = Sysproperties.Id And
16 Syscolumns.Colid = Sysproperties.Smallid)
17
18 Where (Sysobjects.Xtype ='U' Or Sysobjects.Xtype ='V')
19 And Sysobjects.Id = Syscolumns.Id And Systypes.Xtype = Syscolumns.Xtype
20 And Systypes.Name <> 'Sysname' And Sysobjects.Name Like '%' Order By Sysobjects.Name, Syscolumns.Colid

結果如圖:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved