use master
godeclare @dbname varchar(100)
declare @sql varchar(1000)
declare @searchtablename varchar(100)
set @searchtablename='rx_claim_detail'
declare dbname_cursor scroll cursor for
select name from master..sysdatabases order by name
open dbname_cursor
fetch next from dbname_cursor into @dbname
while @@fetch_status=0
begin
fetch next from dbname_cursor into @dbname
set @sql='use ' + @dbname
set @sql=@sql + ' select name as tablename,'''+ @dbname +
''' as dbname from sysobjects where xtype=''u'' and name =''' + @searchtablename + ''' order by name'
begin try
exec(@sql)
end try
begin catch
if(@@error<>0)
begin
print error_message()
endend catch
endclose dbname_cursor
deallocate dbname_cursor
go
修改版:
use mastergo
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#tmpresult'))
begin
drop table #tmpresult
endcreate table #tmpresult
( tablename varchar(100),
dbname varchar(100))go
declare @dbname varchar(100)
declare @sql varchar(1000)
declare @searchtablename varchar(100)
set @searchtablename='rx_claim_detail'
declare dbname_cursor scroll cursor for
select name from master..sysdatabases order by name
open dbname_cursor
fetch next from dbname_cursor into @dbname
while @@fetch_status=0
begin
fetch next from dbname_cursor into @dbname
set @sql='use ' + @dbname
set @sql=@sql + ' insert into #tmpresult select name as tablename,'''+ @dbname +
''' as dbname from sysobjects where xtype=''u'' and name =''' + @searchtablename + ''' order by name'
begin try
exec(@sql)
end try
begin catch
if(@@error<>0)
begin
print error_message()
endend catch
endclose dbname_cursor
deallocate dbname_cursor
goselect * from tempdb..#tmpresult
drop table #tmpresult
修改版2:
use mastergo
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#tmpresult'))
begin
drop table #tmpresult
endif exists(select * from tempdb..sysobjects where id=object_id('tempdb..#tmpresulterrormsg'))
begin
drop table #tmpresulterrormsg
end
create table #tmpresult
( tablename varchar(100),
dbname varchar(100)
) create table #tmpresulterrormsg
(
[useraccount] varchar(50),
[dbname] varchar(50),
[shortissue] varchar(20),
[errormsg] varchar(500)
)
godeclare @serrormsg varchar(500)
declare @suseraccount varchar(50)
declare @sshortissue varchar(20)
declare @dbname varchar(100)
declare @sql varchar(1000)
declare @searchtablename varchar(100)
set @searchtablename='groupprovider'
declare dbname_cursor scroll cursor for
select name from master..sysdatabases order by name
open dbname_cursor
fetch next from dbname_cursor into @dbname
while @@fetch_status=0
begin
fetch next from dbname_cursor into @dbname
set @sql='use ' + @dbname
set @sql=@sql + ' insert into #tmpresult select name as tablename,'''+ @dbname +
''' as dbname from sysobjects where xtype=''u'' and name =''' + @searchtablename + ''' order by name'
begin try
exec(@sql)
end try
begin catch
if(@@error<>0)
begin
print error_message()
set @serrormsg= error_message()
declare @istartpos int=charindex('the server principal "',@serrormsg)
declare @iendpos int=charindex('" is not able to access',@serrormsg)
if(@istartpos>0 and @iendpos>0)
begin
set @suseraccount=substring(@serrormsg,@istartpos + len('the server principal "'),@iendpos-@istartpos-len('" is not able to access')+1)
set @sshortissue ='permission access'
end
else begin
set @sshortissue ='other'
end
--@dbname
insert into #tmpresulterrormsg
select @suseraccount,@dbname,@sshortissue,@serrormsg
endend catch
endclose dbname_cursor
deallocate dbname_cursor
goselect * from tempdb..#tmpresult
select * from tempdb..#tmpresulterrormsg
drop table #tmpresult
drop table #tmpresulterrormsg
<<<<
執行結果:
資料庫查詢表名之 MySql查詢全部表名
最近研究資料庫根據條件查詢表,找了好多地方,發現了很多有關於庫查詢全部表名的例子,但是根據條件查詢部分表名卻很少,我把我找到的分享給大家,希望能夠幫助大家 根據條件 欄位名 查詢資料庫裡面的所有表名 select table name from information schema.columns ...
根據字段內容查詢表名
儲存指定資料庫中的表名 create table t id int identity 1,1 t name varchar 100 null,rc int null 儲存指定表的所有列名 create table t c id int identity 1,1 t c varchar 100 nul...
使用SQL查詢所有資料庫名和表名
show databases select table name from information schema.tables where table schema database name and table type base table select column name from inf...