加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver sp_msforeachtable用法

发布时间:2020-12-12 15:45:27 所属栏目:MsSql教程 来源:网络整理
导读:? 1、???? 输出当前的用户所有的表 ? ? ?sp_msforeachtable ? @command1="print '?'" ? ???? 2、??? 查询数据所有的表和表中的记录数 ?? sp_msforeachtable @command1="select '?' as 表名,count(*) as 记录数 from ?" ? ? 3、?? 数据库所有表中的ABC列,换

?

1、???? 输出当前的用户所有的表

?
? ?sp_msforeachtable
? @command1="print '?'"

?

????

2、??? 查询数据所有的表和表中的记录数

??

sp_msforeachtable
@command1="select '?' as 表名,count(*) as 记录数 from ?"

?

?

3、?? 数据库所有表中的ABC列,换成123

?

exec?? sp_msforeachtable?? @command1=N'??
declare?? @s?? nvarchar(4000),@tbname?? sysname??
select?? @s=N'''',@tbname=N''?''??
select?? @s=@s+N'',''+quotename(a.name)+N''=replace(''+quotename(a.name)+N'',N''''ABC'''',N''''123'''')''??
from?? syscolumns?? a,systypes?? b??
where?? a.id=object_id(@tbname)????
and?? a.xusertype=b.xusertype??
and?? b.name?? like?? N''%char''??
if?? @@rowcount>0??
begin??
set?? @s=stuff(@s,1,N'''')??
print(N''update??
''+@tbname+''?? set?? ''+@s)??

exec(N''update?? ''+@tbname+''?? set?? ''+@s)??
end'??

?

?

4、查询数据库每个表的空间利用情况

?

?

set nocount on exec sp_MSForEachTable @precommand=N'create table ##(id int identity,表名 sysname,字段数 int,记录数 int,保留空间 Nvarchar(10),使用空间 varchar(10),索引使用空间 varchar(10),未用空间 varchar(10))',@command1=N'insert ##(表名,记录数,保留空间,使用空间,索引使用空间,未用空间) exec sp_spaceused ''?''??????? update ## set 字段数=(select count(*) from syscolumns where id=object_id(''?'')) where id=scope_identity()',@postcommand=N'select * from ## order by 记录数 desc drop table ##'set nocount off

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读