SQL Server:视图,union返回的行数不同

时间:2014-04-17 15:45:53

标签: sql sql-server

我对SQL Server并不熟悉,而且我有点卡在这里..

综合观点:

select * 
from dbo.Elen_SalesData_View
where EffectiveDate>='2013-12-01' and EffectiveDate<='2013-12-31'

返回1319行。

Elen_SalesData_View结构:

SELECT     
   ItemCode, ItemDescription, ItemValue, Quantity, 
   CustomerId, CustomerName, SalesInvoiceId, EffectiveDate, 
   CountryId
FROM         
   dbo.Elen_SalesData_Historical

UNION ALL

SELECT     
    ItemCode, ItemDescription, ItemValue, Quantity, 
    CustomerId, CustomerName, SalesInvoiceId, EffectiveDate, 
    CountryId
FROM
    dbo.Elen_SalesData_Current
在这个实例中,

Elen_SalesData_Current将不返回任何内容。所以我们忽略它..

Elen_SalesData_Historical包括:

SELECT     
    CONVERT(varchar(30), ItemCode) AS ItemCode, Product, 
    CONVERT(varchar(60), ItemDescription) AS ItemDescription, 
    ItemValue, Quantity, ItemType, 
    CustomerId, CustomerName, 
    SalesInvoiceId, EffectiveDate, CountryId
FROM         
    db1.dbo.Elen_WinmanSalesArch

返回1319行。

Elen_WinmanSalesArch运行的位置:

select * 
from Elen_WinmanSalesArch
where EffectiveDate>='2013-12-01' and EffectiveDate<='2013-12-31'

返回3489行 - 这是正确的!

为什么呢?以及如何解决它?

也许值得一提的是Elen_WinmanSalesArch正在查询一个数据库,而Elen_SalesData_Historical则是从另一个数据库运行


刚开始测试:

select * from dbo.Elen_SalesData_Historical
where EffectiveDate>='2013-12-01' and EffectiveDate<='2013-12-31'

返回1319行。 Elen_SalesData_Historical是一个视图,其内容如上所述。但是当我跑步时:

SELECT CONVERT(varchar(30), ItemCode) AS ItemCode, Product, 
       CONVERT(varchar(60), ItemDescription) AS ItemDescription, 
       ItemValue, Quantity, ItemType, CustomerId, 
       CustomerName, SalesInvoiceId, EffectiveDate, CountryId 
FROM  db1.dbo.Elen_WinmanSalesArch
where EffectiveDate>='2013-12-01' and EffectiveDate<='2013-12-31'

它返回3489行!我真的很困惑......

0 个答案:

没有答案