代码之家  ›  专栏  ›  技术社区  ›  Frank V

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

  •  4
  • Frank V  · 技术社区  · 15 年前

    我想用 the undocumented system procedure sp_MSforeachtable . 但是,我需要将受影响的表限制为以“smp”开头的表和以“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 回复  |  直到 12 年前
        1
  •  3
  •   devio    15 年前

    更新sql2000:

    declare @s nvarchar(1000)
    set @s = ' and uid = ' + convert(nvarchar, user_id('my_schema'))
    exec sp_msforeachtable @command1='print ''?''', @whereand = @s
    
        2
  •  3
  •   gbn    15 年前

    这应该在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工作

        3
  •  1
  •   Cade Roux    15 年前

    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_%'"
    ---------------------
    
        4
  •  0
  •   Michael Ferrante    15 年前

    此版本在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'' '
    
        5
  •  0
  •   lgowen    12 年前

    这在2008年起作用了R2

    @其中,and='and uid=(从sys.schemas中选择schema_id,其中name='dbo')和o.name,如“temp_%”