--create proc sp_MSforeachtable
declare
@command1 nvarchar(2000), @replacechar nchar(1) , @command2 nvarchar(2000),
@command3 nvarchar(2000) , @whereand nvarchar(2000),
@precommand nvarchar(2000) , @postcommand nvarchar(2000)
--as
set @replacechar = N'?'
set @command1 = null
set @command2 = null
set @command3 = null
set @whereand = null
set @precommand = null
set @postcommand = null
set
@command1= --N'print ''?''',
-- @command2= --"sp_spaceused '?'",
-- @command3=
N'SELECT ''?'' FROM [info].''?'' '
/* 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)))
declare @sql nvarchar(4000)
if (@precommand is not null)
exec(@precommand)
set @sql=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
/* Create the select */
exec(@sql)
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