How to exclude tables from sp_msforeachtable

我是研究僧i 提交于 2019-11-30 17:20:16
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN (
                                                    ISNULL(OBJECT_ID(''[dbo].[T1]''),0),
                                                    ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
                                                   )
                        DELETE FROM ?'
Alex Hinton

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%'''
Hubbitus

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)  
                                                       )'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!