2010-06-30 45 views
2

我想使用sp_msforeachtable爲數據庫中的某些表執行一些工作。我使用IF語句來過濾表。但它並沒有給我正確的答案。如以下腳本所示,我使用AdventureWorks進行測試。我想在每張桌子上做一些工作,除了Person.AddressPerson.ContactPerson.CountryRegion。如你所見,這些表格仍然包含在結果中。爲什麼?誰能幫我解決我的問題?萬分感謝。sp_msforeachtable不給我正確的結果

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] 

回答

3

您是否嘗試過加入括號?

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

上帝,你的解決方案有效。但爲什麼?我們不能在單引號中使用模式名稱和對象名稱嗎? – 2010-06-30 14:38:17

+2

@Yousui:看看輸出 - ms_foreachtable似乎總是使用括號 - 所以你不能比較沒有括號的東西...這只是一個字符串格式化的問題..... – 2010-06-30 14:49:03

+0

appart從無到有,我沒有什麼補充說:) – 2010-06-30 20:05:23

3

爲什麼不直接使用系統表,因爲它顯示了要所有表不是在人模式?

select sys.schemas.name + '.' + sys.tables.name from sys.tables 
    inner join sys.schemas on sys.tables.schema_id = sys.schemas.schema_id 
    where sys.schemas.name <> 'Person'