Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tsql sp_msforeachtable没有给我正确的结果_Tsql_Sql Server 2005_Sp Msforeachtable - Fatal编程技术网

Tsql sp_msforeachtable没有给我正确的结果

Tsql sp_msforeachtable没有给我正确的结果,tsql,sql-server-2005,sp-msforeachtable,Tsql,Sql Server 2005,Sp Msforeachtable,我想使用sp_msforeachtable为数据库中的一些表做一些工作。我使用IF语句过滤表。但它并没有给我正确的答案。如下面的脚本所示,我使用AdventureWorks进行测试。我想在每个表上做一些工作,除了个人。地址,个人。联系人,个人。国家地区。如您所见,这些表仍然包含在结果中。为什么?谁能帮我解决问题?非常感谢 sp_msforeachtable ' IF ''?'' <> ''Person.Address'' AND ''?'' <> ''Person.Con

我想使用sp_msforeachtable为数据库中的一些表做一些工作。我使用IF语句过滤表。但它并没有给我正确的答案。如下面的脚本所示,我使用AdventureWorks进行测试。我想在每个表上做一些工作,除了个人。地址个人。联系人个人。国家地区。如您所见,这些表仍然包含在结果中。为什么?谁能帮我解决问题?非常感谢

sp_msforeachtable '
IF ''?'' <> ''Person.Address'' AND ''?'' <> ''Person.Contact'' AND ''?'' <> ''Person.CountryRegion''
BEGIN
    PRINT ''?''
END
';
sp_msforeachtable'
如果是“”?“”“”人员地址“”和“”?“”“”人员联系人“”和“”?“”“”人员国家地区“”
开始
打印“”?“”
结束
';
结果是:

[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]
[销售][商店]
[制作][ProductPhoto]
[制作][ProductPhoto]
[销售][门店联系人]

[Person].[Address]您是否尝试添加括号

sp_msforeachtable '
IF ''?'' <> ''[Person].[Address]'' AND ''?'' <> ''[Person].[Contact]'' AND ''?'' <> ''[Person].[CountryRegion]''
BEGIN
    PRINT ''?''
END
';
sp_msforeachtable'
如果是“”?“”“”[人员]。[地址]”和“”?“”“”[人员]。[联系人]”和“”?“”“”[人员]。[国家地区]”
开始
打印“”?“”
结束
';

为什么不直接使用系统表,因为您似乎希望所有表都不在Person架构中

    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'
从sys.tables中选择sys.schemas.name+'.+sys.tables.name
sys.tables.schema_id=sys.schemas.schema_id上的内部联接sys.schemas
其中sys.schemas.name“Person”

天哪,你的解决方案行得通。但是为什么呢?我们不能在单引号中使用模式名和对象名吗?@Yousui:看看输出-ms_foreachtable似乎总是使用括号-因此你无法与没有括号的内容进行比较。。。这只是一个字符串格式的问题……我没有什么要补充的:)