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

发布于 2024-07-24 05:30:22 字数 720 浏览 6 评论 0原文

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

I'm attempting to use the undocumented system procedure sp_MSforeachtable. But I need to restrict the affected tables to those that start with "smp" and that are in the "dbo" schema. I was able to find how to find procedures that start with "smp". I simply do:

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

but how do I filter for a given schema using the @whereand parameter?

UPDATE: I tried the following but it didn't work:

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

Update 2: I'm running on SQL Server 2000.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别挽留 2024-07-31 05:30:22

SQL2000 的更新:

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

Update for SQL2000:

declare @s nvarchar(1000)
set @s = ' and uid = ' + convert(nvarchar, user_id('my_schema'))
exec sp_msforeachtable @command1='print ''?''', @whereand = @s
虚拟世界 2024-07-31 05:30:22

这应该适用于 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 工作

This should works in SQL Server 2000 (can't test now):

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

Use OBJECTPROPERTY to find the schema owner id.

Edit: OK, tested it on a SQL 2000 box:

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

I could not get OBJECTPROPERTY to work

回忆那么伤 2024-07-31 05:30:22

来自此处

---------------------
--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_%'"
---------------------

From 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_%'"
---------------------
殊姿 2024-07-31 05:30:22

此版本适用于 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'' '

This verion works in Sql Server 2005:

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

Not exactly sure for Sql Server 2000, but this version might work:

exec sp_MSforeachtable
    @command1=' print ''?''',
    @whereand=' and user_name(uid) = ''dbo'' '
总以为 2024-07-31 05:30:22

这在 2008 R2 中有效

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

This worked in 2008 R2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文