未记录的系统过程'sp_MSforeachtable'和@whereand参数

时间:2009-06-10 18:32:14

标签: sql-server sql-server-2000 system-procedures

我正在尝试使用the undocumented system procedure sp_MSforeachtable。但是我需要将受影响的表限制为以“smp”开头且位于“dbo”模式中的表。我能够找到如何找到以“smp”开头的程序。我只是这样做:

sp_MSforeachtable @command1=' print ''?''', @whereand=' and name like ''smp%''  '

但是如何使用@whereand参数过滤给定的架构?

更新:我尝试了以下操作但不起作用:

sp_MSforeachtable @command1=' print ''?''', @whereand=' and name like ''smp%'' and Left(''?'', 5)=''[dbo]'' '

更新2 :我在SQL Server 2000上运行。

5 个答案:

答案 0 :(得分:3)

SQL2000更新:

declare @s nvarchar(1000)
set @s = ' and uid = ' + convert(nvarchar, user_id('my_schema'))
exec sp_msforeachtable @command1='print ''?''', @whereand = @s

答案 1 :(得分:3)

这应该适用于SQL Server 2000(现在无法测试):

@whereand = '
  AND name like ''smp%'' AND
  OBJECTPROPERTY(OBJECT_ID(''name''), ''OwnerID'') = USER_ID(''dbo'')'

使用OBJECTPROPERTY查找架构所有者ID。

编辑:好的,在SQL 2000框上测试它:

@whereand = ' AND name LIKE ''smp%'' AND uid = 1'
OR
@whereand = ' AND name LIKE ''smp%'' AND USER_ID(''dbo'')'

我无法让OBJECTPROPERTY工作

答案 2 :(得分:1)

来自here

---------------------
--Drop table of particular shcemaID/shemaName and with name starting with 'Temp_'
Exec sp_MSforeachtable @command1 = "DROP TABLE ? PRINT '? dropped'"
    ,@whereand = "and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'dbo')
                  and o.name LIKE 'Temp_%'"
---------------------

答案 3 :(得分:0)

此版本适用于Sql Server 2005:

exec sp_MSforeachtable
    @command1=' print ''?''',
    @whereand=' and schema_name(schema_id) = ''dbo'' '

不完全确定Sql Server 2000,但此版本可能有效:

exec sp_MSforeachtable
    @command1=' print ''?''',
    @whereand=' and user_name(uid) = ''dbo'' '

答案 4 :(得分:0)

这适用于2008 R2

@ whereand ='和uid =(SELECT schema_id FROM sys.schemas WHERE name =''dbo'')和o.name LIKE''TEMP _%'''