4

我正在尝试使用未记录的系统过程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 上运行。

4

5 回答 5

3

SQL2000 更新:

declare @s nvarchar(1000)
set @s = ' and uid = ' + convert(nvarchar, user_id('my_schema'))
exec sp_msforeachtable @command1='print ''?''', @whereand = @s
于 2009-06-10T18:37:27.460 回答
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 工作

于 2009-06-10T18:52:59.737 回答
1

这里

---------------------
--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_%'"
---------------------
于 2009-06-10T18:38:55.647 回答
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'' '
于 2009-06-11T04:41:29.867 回答
0

这在 2008 R2 中有效

@whereand='and uid = (SELECT schema_id FROM sys.schemas WHERE name = ''dbo'') and o.name LIKE ''TEMP_%'''

于 2013-02-06T16:48:02.203 回答