如何从 sp_msforeachtable 中排除表

发布于 2024-12-11 06:36:58 字数 178 浏览 2 评论 0原文

我知道 sp_msforeachtable 允许对所有表执行查询。

我有 100 个表,我想对 97 个表执行相同的查询。

我正在使用以下查询:EXEC sp_MSForEachTable "DELETE FROM ?"

是否可以排除某些表?

I know that sp_msforeachtable allows to perform queries on all tables.

I have 100 tables and I want to perform the same query on 97 tables.

I'm using this query: EXEC sp_MSForEachTable "DELETE FROM ?"

Is it possible to exclude certain tables?

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

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

发布评论

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

评论(3

怼怹恏 2024-12-18 06:36:58
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN (
                                                    ISNULL(OBJECT_ID(''[dbo].[T1]''),0),
                                                    ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
                                                   )
                        DELETE FROM ?'
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN (
                                                    ISNULL(OBJECT_ID(''[dbo].[T1]''),0),
                                                    ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
                                                   )
                        DELETE FROM ?'
π浅易 2024-12-18 06:36:58

我遇到的包含或排除架构和表的最简单语法:

exec sp_MSforeachtable 'print ''?''', 
@whereand='and Schema_Id=Schema_id(''Value'') and o.Name like ''%Value%'''

Simplest syntax I came across to include or exclude schemas and tables:

exec sp_MSforeachtable 'print ''?''', 
@whereand='and Schema_Id=Schema_id(''Value'') and o.Name like ''%Value%'''
够运 2024-12-18 06:36:58

sp_MSforeachtable 是未记录的过程,但根据该示例: http:// avinashkt.blogspot.ru/2008/05/useful-operations-with-spmsforeachtable.html 你可以提供额外的第二个参数@whereand限制表列表。


附加到此的查询如下。

SELECT   '[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + ']' 
       + '.' 
       + '[' + REPLACE(object_name(o.id), N']', N']]') + ']'
FROM   dbo.sysobjects o
       JOIN sys.all_objects syso
         ON o.id = syso.object_id
WHERE  OBJECTPROPERTY(o.id, N'IsUserTable') = 1
       AND o.category & ltrim(str(CONVERT(INT, 0x0002))) = 0 

所以示例语法是

   EXEC sp_MSforeachtable @command1 = N'PRINT ''?'' ', 
                          @whereand = 'AND o.id NOT IN (
                                                     ISNULL(OBJECT_ID(''[dbo].[T1]''),0), 
                                                     ISNULL(OBJECT_ID(''[dbo].[T2]''),0)  
                                                       )'

sp_MSforeachtable is undocumented procedure, but according by that example: http://avinashkt.blogspot.ru/2008/05/useful-operations-with-spmsforeachtable.html you could provide additional second parameter @whereand to limit list of tables.


The query that this gets appended to is the following.

SELECT   '[' + REPLACE(schema_name(syso.schema_id), N']', N']]') + ']' 
       + '.' 
       + '[' + REPLACE(object_name(o.id), N']', N']]') + ']'
FROM   dbo.sysobjects o
       JOIN sys.all_objects syso
         ON o.id = syso.object_id
WHERE  OBJECTPROPERTY(o.id, N'IsUserTable') = 1
       AND o.category & ltrim(str(CONVERT(INT, 0x0002))) = 0 

So example syntax would be

   EXEC sp_MSforeachtable @command1 = N'PRINT ''?'' ', 
                          @whereand = 'AND o.id NOT IN (
                                                     ISNULL(OBJECT_ID(''[dbo].[T1]''),0), 
                                                     ISNULL(OBJECT_ID(''[dbo].[T2]''),0)  
                                                       )'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文