时间:2020-03-10来源:电脑系统城作者:电脑系统城
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/*************1:删除临时表*************/if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#tempTable')) drop table #tempTable;/*************2:定义游标*************/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);/*************3:打开游标*************/open databaseNameCursor;/*************4:连接游标*************/fetch next from databaseNameCursor into @databaseName/*************5:创建临时表*************/create table #tempTable(id int identity(1,1) not null,databasename nvarchar(max),schemaname nvarchar(max),tablename nvarchar(max),primary key(id));/************6:循环插入临时表中*************/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/*************7:关闭游标*************/close databaseNameCursor;/*************8:释放游标*************/deallocate databaseNameCursor;/*************9:查看服务器所有表*************/select * from #tempTable |
先执行注释1,然后注释2到注释8脚本一起执行,最后执行注释9或者使用临时表。

2023-10-27
windows11安装SQL server数据库报错等待数据库引擎恢复句柄失败解决办法2023-10-27
SQL Server截取字符串函数操作常见方法2023-10-27
浅谈SELECT *会导致查询效率低的原因收缩数据文件通过将数据页从文件末尾移动到更靠近文件开头的未占用的空间来恢复空间,在文件末尾创建足够的空间后,可取消对文件末尾的数据页的分配并将它们返回给文件系统,本文给大家介绍SQL Server 数据库中的收缩数据...
2023-10-27