iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >SQLServer之查询当前服务器下所有目录视图表
  • 552
分享到

SQLServer之查询当前服务器下所有目录视图表

SQLServer之查询当前服务器下所有目录视图表 2020-09-11 02:09:38 552人浏览 无得
摘要

sql脚本 if exists(select * from tempdb..sysobjects where id=object_id("tempdb..#tempTable")) drop t

SQLServer之查询当前服务器下所有目录视图表

sql脚本


if exists(select * from tempdb..sysobjects where id=object_id("tempdb..#tempTable"))
	drop table #tempTable;


declare databaseNameCursor cursor for select name from master.dbo.SysDatabases;
declare @databaseName nvarchar(512),@databaseCount int;
set @databaseCount=(select count(1) from master.dbo.SysDatabases);


open databaseNameCursor;


fetch next from databaseNameCursor into @databaseName


create table #tempTable
(
id int identity(1,1) not null,
databasename nvarchar(max),
schemaname nvarchar(max),
tablename nvarchar(max),
primary key(id)
);


while (@@fetch_status=0 and @databaseCount>0)
	begin 
	    begin try
			set @databaseCount=@databaseCount-1;

			declare @tableFullName nvarchar(1024);
			set @tableFullName="select """+@databaseName+""",schema_name(schema_id),name from "+@databaseName+".sys.tables";

			insert into #tempTable(databasename,schemaname,tablename)
			exec sp_executesql  @tableFullName;
			--指向下一个游标
			fetch next from databaseNameCursor into @databaseName  

		end  try
		begin catch
			continue;
		end catch
	end


close databaseNameCursor;


deallocate databaseNameCursor;


select * from #tempTable 

  

SQL脚本使用

先执行注释1,然后注释2到注释8脚本一起执行,最后执行注释9或者使用临时表。

SQL执行结果

 

 

 

--结束END--

本文标题: SQLServer之查询当前服务器下所有目录视图表

本文链接: https://www.lsjlt.com/news/4605.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作