t-sql - sp_msforeachtable 没有给我正确的结果

标签 t-sql sql-server-2005 sp-msforeachtable

我想使用 sp_msforeachtable 对数据库中的某些表执行一些操作。我使用 IF 语句来过滤表。但它没有给我正确的答案。如以下脚本所示,我使用 AdventureWorks 进行测试。我想对除 Person.AddressPerson.ContactPerson.CountryRegion 之外的每个表进行一些工作。正如您所看到的,这些表仍然包含在结果中。为什么?谁能帮助我解决我的问题?非常感谢。

sp_msforeachtable '
IF ''?'' <> ''Person.Address'' AND ''?'' <> ''Person.Contact'' AND ''?'' <> ''Person.CountryRegion''
BEGIN
    PRINT ''?''
END
';

结果是:

[Sales].[Store]
[Production].[ProductPhoto]
[Production].[ProductProductPhoto]
[Sales].[StoreContact]
[Person].[Address] <------------------------------
[Production].[ProductReview]
[Production].[TransactionHistory]
[Person].[AddressType]
[Production].[ProductSubcategory]
[dbo].[AWBuildVersion]
[Production].[TransactionHistoryArchive]
[Purchasing].[ProductVendor]
[Production].[BillOfMaterials]
[Production].[UnitMeasure]
[Purchasing].[Vendor]
[Purchasing].[PurchaseOrderDetail]
[Person].[Contact] <------------------------------
[Purchasing].[VendorAddress]
[Purchasing].[VendorContact]
[Purchasing].[PurchaseOrderHeader]
[Sales].[ContactCreditCard]
[Production].[WorkOrder]
[Person].[ContactType]
[Sales].[CountryRegionCurrency]
[Production].[WorkOrderRouting]
[Person].[CountryRegion] <------------------------------
[Sales].[CreditCard]
[Production].[Culture]
[Sales].[Currency]
[Sales].[SalesOrderDetail]
[Sales].[CurrencyRate]
[Sales].[Customer]
[Sales].[SalesOrderHeader]
[Sales].[CustomerAddress]
[HumanResources].[Department]
[Production].[Document]
[HumanResources].[Employee]
[Sales].[SalesOrderHeaderSalesReason]
[Sales].[SalesPerson]
[HumanResources].[EmployeeAddress]
[HumanResources].[EmployeeDepartmentHistory]
[HumanResources].[EmployeePayHistory]
[Sales].[SalesPersonQuotaHistory]
[Production].[Illustration]
[Sales].[SalesReason]
[Sales].[Individual]
[Sales].[SalesTaxRate]
[HumanResources].[JobCandidate]
[Production].[Location]
[Sales].[SalesTerritory]
[Production].[Product]
[Sales].[SalesTerritoryHistory]
[Production].[ScrapReason]
[HumanResources].[Shift]
[Production].[ProductCategory]
[Purchasing].[ShipMethod]
[Production].[ProductCostHistory]
[Production].[ProductDescription]
[Sales].[ShoppingCartItem]
[Production].[ProductDocument]
[Production].[ProductInventory]
[Sales].[SpecialOffer]
[Production].[ProductListPriceHistory]
[Sales].[SpecialOfferProduct]
[Production].[ProductModel]
[Person].[StateProvince]
[Production].[ProductModelIllustration]
[dbo].[DatabaseLog]
[Production].[ProductModelProductDescriptionCulture]
[dbo].[ErrorLog]

最佳答案

您尝试过添加括号吗?

sp_msforeachtable '
IF ''?'' <> ''[Person].[Address]'' AND ''?'' <> ''[Person].[Contact]'' AND ''?'' <> ''[Person].[CountryRegion]''
BEGIN
    PRINT ''?''
END
';

关于t-sql - sp_msforeachtable 没有给我正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3150151/

相关文章:

sql-server - SQL Server : How to make server check all its check constraints?

sql - 如何使用 sp_MSforeachtable 执行多行查询

sql - 我可以有一个命名整型常量以便在 T-SQL 中跨存储过程使用吗?

SQL-临时表 : Storing all columns in temp table versus only Primary key

sql-server-2005 - 在 MS-SQL Server 中,如何将数据库用户从 WithoutLogin 更改为与登录名关联

sql - 如何使用 T-SQL 获取在主键字段中生成的最后一个值

sql - 如何从 sp_msforeachtable 中排除表

sql - 与稍后提供的一起加入

sql - 如何在哪个数据库中发现名称为 'myStoredProcedure' 的存储过程?

sql-server - 将分区移动到新文件组