[转]sp_MSforeachObject

CREATE   proc  sp_MSforeachObject 
 
@objectType   int = 1
 
@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  
/*  
EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4 
         1    'IsUserTable'  表 
         2    'IsView'  视图 
         3    'IsTrigger'  触发器 
         4    'IsProcedure'   存储过程 
         5    'IsDefault'   
         6    'IsForeignKey'  外键 
         7    'IsScalarFunction' 自定义函数 
         8    'IsInlineFunction' 
         9    'IsPrimaryKey' 
         10  'IsExtendedProc'     主键 
         11  'IsReplProc' 
         12  'IsRule'   自定义规则 
*/  
 
/*  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
 
/*  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  

转载于:https://www.cnblogs.com/treeyh/archive/2007/08/20/862943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值