sql server實現(xiàn)在多個數(shù)據(jù)庫間快速查詢某個表信息的方法
本文實例講述了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ù)庫 go exec 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ù)庫進(jìn)行查詢 得到哪個數(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) begin print '' print '' print '當(dāng)前數(shù)據(jù)庫: '+ @DataBaseName --讀出每個數(shù)據(jù)庫的名字 insert into #TableInfo --把存儲過程查詢出來的數(shù)據(jù)插進(jìn)臨時表 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)移動 end close 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) --查詢指定名字的表 begin print '====================當(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ù)庫 end else begin print '--------------------當(dāng)前服務(wù)器不存在 ' + @TableName + ' 表--------------------' end drop table #TableInfo --刪除臨時表
更多關(guān)于SQL Server相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《SQL Server查詢操作技巧大全》、《SQL Server存儲過程技巧大全》、《SQL Server索引操作技巧大全》、《SQL Server常用函數(shù)匯總》及《SQL Server日期與時間操作技巧總結(jié)》
希望本文所述對大家SQL Server數(shù)據(jù)庫程序設(shè)計有所幫助。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。