国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

sql server實現(xiàn)在多個數(shù)據(jù)庫間快速查詢某個表信息的方法

2024-08-31 01:04:45
字體:
供稿:網(wǎng)友

本文實例講述了sql server實現(xiàn)在多個數(shù)據(jù)庫間快速查詢某個表信息的方法。分享給大家供大家參考,具體如下:

最近出來實習(xí),所在公司的服務(wù)器有十幾個數(shù)據(jù)庫,為了方便根據(jù)某個數(shù)據(jù)表的  表名  快速找到對應(yīng)的數(shù)據(jù)庫,又復(fù)習(xí)了一下游標(biāo)的知識,寫了下面這個sql代碼,方便自己的工作。

1.先了解一下系統(tǒng)存儲過程和系統(tǒng)表的使用,簡單介紹一下我用到的幾個系統(tǒng)存儲過程(資料參考網(wǎng)絡(luò))

use master --切換到系統(tǒng)數(shù)據(jù)庫,因為下面用到的系統(tǒng)存儲過程和系統(tǒng)表大部分存在于該數(shù)據(jù)庫goexec sp_helpdb --查詢 當(dāng)前 服務(wù)器的所有數(shù)據(jù)庫select [name] from [sysdatabases] --查詢 當(dāng)前 服務(wù)器的所有數(shù)據(jù)庫select * from sysobjects where type = 'u'--列出 當(dāng)前 數(shù)據(jù)庫里所有的表名select * from information_schema.tables --列出 當(dāng)前 數(shù)據(jù)庫里所有的表名(執(zhí)行對比一下與上面這個語句的查詢結(jié)果)select * from syscolumns where id = object_id('spt_fallback_db') --列出指定表里的所有的信息,包括字段等等(根據(jù)需要修改參數(shù))

2.直接上代碼(具體請參考注釋,純屬學(xué)習(xí),有錯請指出)

use master --切換到系統(tǒng)數(shù)據(jù)庫,因為下面用到的  系統(tǒng)存儲過程和系統(tǒng)表  大部分存在于該數(shù)據(jù)庫go------------------在當(dāng)前服務(wù)器  根據(jù)表的名字  在多個數(shù)據(jù)庫進行查詢  得到哪個數(shù)據(jù)庫存在該表的信息------------------declare @DataBaseName nvarchar(max) --定義變量(數(shù)據(jù)庫的名字)declare cur cursor for select [name] from [sysdatabases] --定義游標(biāo),該游標(biāo)指向  當(dāng)前  服務(wù)器上的所有數(shù)據(jù)庫名字列表open cur --打開游標(biāo)create table #TableInfo (table_catalog nvarchar(max),table_schema nvarchar(max),table_name nvarchar(max),table_type nvarchar(max)) --創(chuàng)建臨時表用于存儲所有數(shù)據(jù)庫的所有表信息fetch next from cur into @DataBaseName --獲取游標(biāo)的數(shù)據(jù),相當(dāng)于獲取數(shù)據(jù)庫名字列表的第一條數(shù)據(jù)while (@@fetch_status=0)beginprint ''print ''print '當(dāng)前數(shù)據(jù)庫: '+ @DataBaseName --讀出每個數(shù)據(jù)庫的名字insert into #TableInfo --把存儲過程查詢出來的數(shù)據(jù)插進臨時表exec('select table_catalog,table_schema,table_name,table_type from ' + @DataBaseName + '.information_schema.tables') --查詢對應(yīng)數(shù)據(jù)庫的所有表print '--------------------------------------------------------------------------------------------------------------------------------------'fetch next from cur into @DataBaseName --游標(biāo)移動endclose cur --關(guān)閉游標(biāo)deallocate cur --釋放游標(biāo)print ''print ''print ''print ''print ''declare @TableName nvarchar(max)set @TableName = 'MyTableName' --查詢條件(根據(jù)需要自行修改)if exists(select table_name from #TableInfo where table_name = @TableName) --查詢指定名字的表beginprint '====================當(dāng)前服務(wù)器存在 ' + @TableName + ' 表,相關(guān)信息請到結(jié)果窗口查看===================='select table_catalog as '所屬數(shù)據(jù)庫',table_name as '表名' from #TableInfo where table_name = @TableName --輸出表的相關(guān)信息,從這些信息就可以知道這個表在哪個數(shù)據(jù)庫endelsebeginprint '--------------------當(dāng)前服務(wù)器不存在 ' + @TableName + ' 表--------------------'enddrop table #TableInfo --刪除臨時表

希望本文所述對大家SQL Server數(shù)據(jù)庫程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到MSSQL教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛冈县| 禹州市| 盱眙县| 富顺县| 荆州市| 藁城市| 鄂伦春自治旗| 乌审旗| 中方县| 夏津县| 黑河市| 法库县| 安多县| 襄汾县| 措勤县| 如皋市| 新蔡县| 拜城县| 肥乡县| 滦南县| 晋州市| 慈利县| 浦东新区| 卢湾区| 黄龙县| 安丘市| 修文县| 教育| 洪湖市| 资兴市| 金平| 英山县| 驻马店市| 大荔县| 汨罗市| 城市| 榆树市| 浏阳市| 湖南省| 大同县| 贵南县|