通過系統(tǒng)數(shù)據(jù)庫獲取用戶所有數(shù)據(jù)庫中的視圖、表、存儲過程
--設(shè)置sql 可以獲取實例名稱
sp_configure 'xp_cmdshell' , 1;
go
reconfigure;
go
--獲取實例名
EXEC sys .xp_cmdshell 'sqlcmd -Lc'
--獲取所有數(shù)據(jù)庫
Select * FROM Master..SysDatabases order by Name
--獲取數(shù)據(jù)庫所有表
use yeekang ---數(shù)據(jù)庫名稱
select * from sysobjects where type = 'U' order by name
--獲取指定表字段
select * from syscolumns where id= OBJECT_ID('Userinfo' )
SELECT
表名 =case when a .colorder= 1 then d.name else '' end ,
表說明 =case when a .colorder= 1 then isnull(f .value, '') else '' end,
字段序號 =a. colorder,
字段名 =a. name,
標(biāo)識 =case when COLUMNPROPERTY ( a .id, a.name ,'IsIdentity')= 1 then '√' else '' end ,
主鍵 =case when 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
))) then '√ ' else '' end,
類型 =b. name,
占用字節(jié)數(shù) =a. length,
長度 =COLUMNPROPERTY( a.id ,a. name,'PRECISION' ),
小數(shù)位數(shù) =isnull( COLUMNPROPERTY(a .id, a.name ,'Scale'), 0),
允許空 =case when a .isnullable= 1 then '√' else '' end,
默認(rèn)值 =isnull( e.text ,''),
字段說明 =isnull( g.[value] ,'')
FROM syscolumns a
left join systypes b on a. xusertype=b .xusertype
inner join sysobjects d on a. id=d .id and d .xtype= 'U' and d.name <>'dtproperties'
left join syscomments e on a. cdefault=e .id
left join sys. extended_properties g on a.id =g. major_id and a.colid =g. minor_id
left join sys. extended_properties f on d.id =f. major_id and f. minor_id=0
where a .id= OBJECT_ID('Userinfo' )
order by a. id,a .colorder
另一例:
在SQL Server數(shù)據(jù)庫中每一個數(shù)據(jù)庫都有一個sysobjects系統(tǒng)表,這個表里面存儲了當(dāng)前數(shù)據(jù)庫的所有對象,包括對象表,用戶表,視圖,觸發(fā)器,約束,默認(rèn)值,日志,和存儲過程的信。
先列一下這張表的一些字段名:
name 對象名
id 對象標(biāo)識號
xtype 對象類型
uid 所有者對象的用戶ID
parent_obj 父對象的對象標(biāo)識號
crdate 對象的創(chuàng)建日期
ftcatid 為全文索引注冊的所有用戶表的全文目錄標(biāo)識符
schema_ver 版本號,
category 用于發(fā)布,約束和標(biāo)識
看上上面的字段你應(yīng)該已經(jīng)清楚了吧...
xtype這個字段就是確定對象類型的...
如果你想獲取數(shù)據(jù)庫中所有的表信息,你可以寫如下的查詢語句:
select * from sysobjects where xtype='u'
如果你想獲取數(shù)據(jù)庫中所有的存儲過程的信息,你可以寫如下的查詢語句:
select * from sysobjects where xtype='p'
如果你想獲取數(shù)據(jù)庫中所有的視圖信息,你可以寫如下的查詢語句:
select * from sysobjects where xtype='v'
如果你想獲取數(shù)據(jù)庫中所有的觸發(fā)器信息,你可以寫如下的查詢語句:
select * from sysobjects where xtype='tr'
獲取SQL所有數(shù)據(jù)庫名、所有表名、所有字段名
1.獲取所有數(shù)據(jù)庫名:
Select Name FROM Master..SysDatabases orDER BY Name
2.獲取所有表名:
Select Name FROM DatabaseName..SysObjects Where XType='U' orDER BY Name
XType='U':表示所有用戶表;
XType='S':表示所有系統(tǒng)表;
3.獲取所有字段名:
Select Name FROM SysColumns Where id=Object_Id('TableName')
方法二。
select * from sys.databases
select * from information_schema.tables
select * from information_schema.columns where table_name='tablename'
相關(guān)文章
SQLSERVER 根據(jù)地圖經(jīng)緯度計算距離差示例
SQL SERVER 根據(jù)地圖經(jīng)緯度計算距離及其公式如下,需要的朋友可以參考下2013-08-08使用SqlBulkCopy時應(yīng)注意Sqlserver表中使用缺省值的列
今天,想將以前做的一個程序增加點功能,原本就使用SqlBulkCopy批量、定時的從目錄中的txt文件導(dǎo)入數(shù)據(jù)到Sqlserver中。以前一直都使用正常,但是不知怎的就老是出現(xiàn)一個錯誤2012-07-07SQL Server使用Merge語句當(dāng)源表數(shù)據(jù)集為空時,無法進(jìn)行查詢的問題
今天使用Merge語句時遇到了一個問題,當(dāng)源表數(shù)據(jù)集為空時,merge就不工作了。2009-07-07sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法
這篇文章介紹了sql注入數(shù)據(jù)庫修復(fù)的兩種實例方法,有需要的朋友可以參考一下2013-09-09sql IDENTITY_INSERT對標(biāo)識列的作用和使用
本文主要介紹了sql IDENTITY_INSERT對標(biāo)識列的作用和使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01SQL Server 2012 FileTable 新特性詳解
FileTable是基于FILESTREAM的一個特性。本文給大家介紹SQL Server 2012 FileTable 新特性詳解,非常不錯,感興趣的朋友一起學(xué)習(xí)吧2016-08-08