大家好 每日一练又来了 请大家高抬贵手 进来写一下这两个存储过程的用法

wuxinyuzhu 2008-01-14 03:06:33

如题 这两个存储过程的用法 顺便写也实例
sp_MSforeachtable


sp_Msforeachdb
...全文
150 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
utpcb 2008-01-14
  • 打赏
  • 举报
回复
UP
wuxinyuzhu 2008-01-14
  • 打赏
  • 举报
回复
继续啊高手们 就等着你们回答了啊
areswang 2008-01-14
  • 打赏
  • 举报
回复
1
liangCK 2008-01-14
  • 打赏
  • 举报
回复
--得到数据库中所有表的空间/记录情况

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 id drop table ##'
liangCK 2008-01-14
  • 打赏
  • 举报
回复
SQL Server数据库检修    

zjcxc(原作)


使用数据库的过程中,由于断电或其他原因,有可能导致数据库出现一些小错误,比如检索某些表特别慢,查询不到符合条件的数据等.

出现这些情况的原因,往往是因为数据库有些损坏,或索引不完整.

在ACCESS中,有个修复数据库的功能可以解决这个问题,在SQL企业管理器,没有这个功能,要用语句来完成,下面就介绍如何用 SQL 语句完成数据库的修复,需要注意的是,在进行下面的操作时,必须断开所有用户的连接:

USE MASTER
GO

sp_dboption '你的数据库名', 'single user', 'true'
Go

DBCC CHECKDB('你的数据库名', REPAIR_REBUILD)
Go

USE 你的数据库名
go

exec sp_msforeachtable 'DBCC CHECKTABLE(''?'',REPAIR_REBUILD)'
go

sp_dboption '你的数据库名', 'single user', 'false'
Go
-狙击手- 2008-01-14
  • 打赏
  • 举报
回复
sp_MSforeachD B除了@whereand外,和 sp_MSforeachtable 的参数是一样的
我们可以通过这个存储过程检测所有的数据库
-狙击手- 2008-01-14
  • 打赏
  • 举报
回复
检查所有的数据库
EXEC sp_MSforeachdb @command1='print ''?''',
@command2='DBCC CHECKDB (?) '
liangCK 2008-01-14
  • 打赏
  • 举报
回复
sp_MSforeachtable
这个系统存储过程有7个参数:
@command1 nvarchar(2000), --第一条运行的T-SQL指令
@replacechar nchar(1) = N'?', --指定的占位符号
@command2 nvarchar(2000) = null, --第二条运行的T-SQL指令
@command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
@whereand nvarchar(2000) = null, --可选条件来选择表
@precommand nvarchar(2000) = null, --在表前执行的指令
@postcommand nvarchar(2000) = null --在表后执行的指令


使用示例:

--1.获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "

--2.更新PUBS数据库中已t开头的所有表的统计:
EXEC sp_MSforeachtable @whereand='and name like ''t%''',
@replacechar='*',
@precommand='print ''Updating Statistics.....''',
@command1='print ''*'' update statistics * ',
@postcommand= 'print ''Complete Update Statistics!'''


==========================================================================

建立自己的sp_MSforeachObject For zj:

USE MASTER
GO

CREATE proc sp_MSforeachObject
@objectType nvarchar(500)='U', --对象类型,可以是下列对象类型中的一种或组合:
--C = CHECK 约束
--D = 默认值或 DEFAULT 约束
--F = FOREIGN KEY 约束
--L = 日志
--FN = 标量函数
--IF = 内嵌表函数
--P = 存储过程
--PK = PRIMARY KEY 约束(类型是 K)
--RF = 复制筛选存储过程
--S = 系统表
--TF = 表函数
--TR = 触发器
--U = 用户表
--UQ = UNIQUE 约束(类型是 K)
--V = 视图
--X = 扩展存储过程
@command1 nvarchar(2000),
@replacechar nchar(1) = N'?',
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Create the select */
/* Use @isobject variable isstead of IsUserTable string */
set @objectType=''''+replace(@objectType,',',''',''')+''''
EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +
REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where o.xtype in('+@objectType+') '+N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)

declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval
GO

===================================================================================

USE MASTER
GO
SP_HELPTEXT sp_MSforeachtable

--下面时sp_MSforeachtable的原始代码

CREATE proc sp_MSforeachtable
@command1 nvarchar(2000),
@replacechar nchar(1) = N'?',
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table
(optionally, matching @where), with each table
defaulting to its own result set */

/* @precommand and @postcommand may be used to force a single
result set via a temp table. */

/* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Create the select */
exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N''IsUserTable'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)

declare @retval int
select @retval = @@error

if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval
go

sp_MSforeachD B除了@whereand外,和 sp_MSforeachtable 的参数是一样的
我们可以通过这个存储过程检测所有的数据库,比如:

--2.检查所有的数据库
EXEC sp_MSforeachdb @command1='print ''?''',
@command2='DBCC CHECKDB (?) '

==============================================================================
建立自己的sp_MSforeachObject:

USE MASTER
GO

CREATE proc sp_MSforeachObject
@objectType int=1, --对象类型,可以是下列对象类型中的一种或组合:
--C = CHECK 约束
--D = 默认值或 DEFAULT 约束
--F = FOREIGN KEY 约束
--L = 日志
--FN = 标量函数
--IF = 内嵌表函数
--P = 存储过程
--PK = PRIMARY KEY 约束(类型是 K)
--RF = 复制筛选存储过程
--S = 系统表
--TF = 表函数
--TR = 触发器
--U = 用户表
--UQ = UNIQUE 约束(类型是 K)
--V = 视图
--X = 扩展存储过程
@command1 nvarchar(2000),
@replacechar nchar(1) = N'?',
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Defined @isobject for save object type */
Declare @isobject varchar(256)

select @isobject= case @objectType when 1 then 'IsUserTable'
when 2 then 'IsView'
when 3 then 'IsTrigger'
when 4 then 'IsProcedure'
when 5 then 'IsDefault'
when 6 then 'IsForeignKey'
when 7 then 'IsScalarFunction'
when 8 then 'IsInlineFunction'
when 9 then 'IsPrimaryKey'
when 10 then 'IsExtendedProc'
when 11 then 'IsReplProc'
when 12 then 'IsRule'
end

/* Create the select */
/* Use @isobject variable isstead of IsUserTable string */
EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +

REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)

declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval
GO

这样我们来测试一下:
1.获得所有的存储过程的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4
2.获得所有的视图的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2

3.比如在开发过程中,没一个用户都是自己的OBJECT OWNER,所以在真实的数据库时都要改为DBO:
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=1
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=2
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=3
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4

这样就非常方便的将每一个数据库对象改为DBO.

当然还要很多非常好的功能,大家可以自己深入研究吧:-)











对该文的评论 人气:137
-狙击手- 2008-01-14
  • 打赏
  • 举报
回复
sp_MSforeachtable
这个系统存储过程有7个参数:
@command1 nvarchar(2000), --第一条运行的T-SQL指令
@replacechar nchar(1) = N'?', --指定的占位符号
@command2 nvarchar(2000) = null, --第二条运行的T-SQL指令
@command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
@whereand nvarchar(2000) = null, --可选条件来选择表
@precommand nvarchar(2000) = null, --在表前执行的指令
@postcommand nvarchar(2000) = null --在表后执行的指令


使用示例:

--1.获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "

--2.更新PUBS数据库中已t开头的所有表的统计:
EXEC sp_MSforeachtable @whereand='and name like ''t%''',
@replacechar='*',
@precommand='print ''Updating Statistics.....''',
@command1='print ''*'' update statistics * ',
@postcommand= 'print ''Complete Update Statistics!'''


==========================================================================
pt1314917 2008-01-14
  • 打赏
  • 举报
回复
什么?

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧