將SQL Server中所有表的列資訊顯示出來

2021-09-05 18:57:52 字數 1205 閱讀 6030

正在作乙個關於sql server資料庫匯入excel檔案的程式,要讀取資料庫中的列的資訊,從網上找了很多資料,終於總結出來比較理想的sql語句,執行後返回的列分別是:表名、列名、列型別、列長度、列描述、是否主鍵,語句如下:

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,

case when syscolumns.name in

(select 主鍵=a.name

from syscolumns a

inner join sysobjects b on a.id=b.id and b.xtype='u' and b.name<>'dtproperties'

where exists(select 1 from sysobjects where xtype='pk' and name in (

select name from sysindexes where indid in(

select indid from sysindexkeys where id = a.id and colid=a.colid

)))

and b.name=sysobjects.name

) then 1 else 0 end as is_key 

from sysobjects,systypes,syscolumns

left join sysproperties on (syscolumns.id = sysproperties.id and

syscolumns.colid = sysproperties.smallid) 

where (sysobjects.xtype ='u' or sysobjects.xtype ='v')

and sysobjects.id = syscolumns.id and systypes.xtype = syscolumns.xtype

and systypes.name <> 'sysname' and sysobjects.name like '%' order by sysobjects.name, syscolumns.colid

結果如圖:

將SQL SERVER中所有表的列資訊顯示出來

將sql server中所有表的列資訊顯示出來 正在作乙個關於sql server資料庫匯入excel檔案的程式,要讀取資料庫中的列的資訊,從網上找了很多資料,終於總結出來比較理想的sql語句,執行後返回的列分別是 表名 列名 列型別 列長度 列描述 是否主鍵,語句如下 sysobjects.nam...

清空SqlServer中所有表的資料

sp msforeachtable command1 delete from sp msforeachtable使用方法 1 說明 系統儲存過程sp msforeachtable和sp msforeachdb,是微軟提供的兩個不公開的儲存過程,從ms sql 6.5開始。存放在sql server的...

清空SQL Server資料庫中所有表資料的方法

原文 清空sql server資料庫中所有表資料的方法 其實刪除資料庫中資料的方法並不複雜,為什麼我還要多此一舉呢,一是我這裡介紹的是刪除資料庫的所有資料,因為資料之間可能形成相互約束關係,刪除操作可能陷入死迴圈,二是這裡使用了微軟未正式公開的sp msforeachtable儲存過程。也許很多讀者...