SQL Server视图未获得任何结果

时间:2013-01-18 16:25:33

标签: sql view sql-server-2008-r2

我是SQL Server的新手,我正在尝试创建一个包含所有客户信息的视图。

如果客户存在,Customer是我想要在所有其他表中引入的主表。

我尝试使用SQL Server Management Studio,但它没有给我带来任何结果。

这就是我选择的表格:

enter image description here

我希望Corporation中的分支和AccountNumber中的Account以及其他表中的所有其他列。

这就是管理工作室给我的:

SELECT     
   Customer.Customer.*, Customer.Miscellaneous2.*, Common.Address.*, 
   Company.CorporationStructure.Branch AS Expr1, Customer.Delivery.*, 
   Customer.Miscellaneous.*, Customer.Account.AccountNumber AS Expr2 
FROM         
   Customer.Account 
INNER JOIN
   Common.Address ON Customer.Account.AccountId = Common.Address.ForeignId 
INNER JOIN
   Company.CorporationStructure ON Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId 
                       AND Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId 
 INNER JOIN
    Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
 INNER JOIN
    Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch 
 INNER JOIN
    Customer.Miscellaneous ON Company.CorporationStructure.Branch = Customer.Miscellaneous.Branch 
 INNER JOIN
    Customer.Miscellaneous2 ON Company.CorporationStructure.Branch = Customer.Miscellaneous2.Branch

返回零结果。

任何人都能看到我做错了什么?

=== UPDATE ===

我按照建议更新了查询:

SELECT     Customer.Customer.IsActive, Customer.Customer.CompanyCode, Customer.Customer.Branch, Customer.Customer.Account, Customer.Customer.TypeCompanyTank, 
                      Customer.Customer.SalesManCode, Customer.Customer.Name, Customer.Customer.Street, Customer.Customer.City, Customer.Customer.State, 
                      Customer.Customer.ZipCode, Customer.Customer.ZipCodeExtension, Customer.Customer.AreaCode, Customer.Customer.PhoneNumber, Customer.Customer.TaxCode, 
                      Customer.Customer.CreditCode, Customer.Customer.Type1, Customer.Customer.FinanceChargeCode, Customer.Customer.StatementInvoiceCode, 
                      Customer.Customer.OpenItemBalanceForward, Customer.Customer.BillingTransaction, Customer.Customer.BudgetChange, Customer.Customer.DelinquentLetter, 
                      Customer.Customer.PriceClassA, Customer.Customer.PriceClassB, Customer.Customer.PriceClassC, Customer.Customer.PriceClassR, 
                      Customer.Customer.InvoiceTerms, Customer.Customer.DefaultProductCode, Customer.Customer.DefaultUnitOfIssue, Customer.Customer.Type2, 
                      Customer.Customer.BillingCycle, Customer.Customer.CurrentBalance, Customer.Customer.Over30, Customer.Customer.Over60, Customer.Customer.Over90, 
                      Customer.Customer.Over120, Customer.Customer.PreviousStatmentBalance, Customer.Customer.DepositAmount, Customer.Customer.LastPaymenRecievedDate, 
                      Customer.Customer.LastPaymentRecievedAmount, Customer.Customer.BudgetRate, Customer.Customer.NonBudgetBalance, Customer.Customer.BudgetBalance, 
                      Customer.Customer.UnpaidFinanceCharges, Customer.Customer.StartDate, Customer.Customer.CreditRecord, Customer.Customer.CreditLimit, 
                      Customer.Miscellaneous2.IsActive AS Expr3, Customer.Miscellaneous2.CompanyCode AS Expr4, Customer.Miscellaneous2.Branch AS Expr5, 
                      Customer.Miscellaneous2.Account AS Expr6, Customer.Miscellaneous2.BillingCycleCode, Customer.Miscellaneous2.LatitudeDegree, 
                      Customer.Miscellaneous2.LatitudeDecimal, Customer.Miscellaneous2.North, Customer.Miscellaneous2.LongitudeDegree, 
                      Customer.Miscellaneous2.LongitudeDecimal, Customer.Miscellaneous2.West, Customer.Miscellaneous2.GEOResultCode, Customer.Miscellaneous2.DeliveryState, 
                      Customer.Miscellaneous2.DeliveryZipCode, Customer.Miscellaneous2.DeliveryZipCodeExtension, Customer.Miscellaneous2.RouteBook, 
                      Customer.Miscellaneous2.DriverRouteSequence, Customer.Miscellaneous2.StandingPurchaseOrderNumber, Customer.Miscellaneous2.PhoneType2, 
                      Customer.Miscellaneous2.AreaCode2, Customer.Miscellaneous2.PhoneNumber2, Customer.Miscellaneous2.PhoneType3, Customer.Miscellaneous2.PhoneNumber3, 
                      Customer.Miscellaneous2.EmailAddress, Customer.Miscellaneous2.ElectronicLetterOption, Customer.Miscellaneous2.ElectronicPostcardOption, 
                      Customer.Miscellaneous2.ElectronicStatementOption, Customer.Miscellaneous2.ElectronicInvoiceOption, Customer.Miscellaneous2.LossReason, 
                      Customer.Miscellaneous2.LossYear, Customer.Miscellaneous2.LossMonth, Customer.Miscellaneous2.GainReason, Customer.Miscellaneous2.GainYear, 
                      Customer.Miscellaneous2.GainMonth, Customer.Miscellaneous2.LastSalesActivityDate, Customer.Miscellaneous2.ElectricTankMonitorNumber, 
                      Common.Address.AddressId, Common.Address.ForeignId, Common.Address.AddressType, Common.Address.Street AS Expr7, Common.Address.City AS Expr8, 
                      Common.Address.State AS Expr9, Common.Address.ZipCode AS Expr10, Common.Address.ZipCodeExtension AS Expr11, Common.Address.FromFlatFile, 
                      Common.Address.IsUpdated, Common.Address.IsAdded, Common.Address.ClearUpdateAndAdded, Common.Address.CorporationId, 
                      Company.CorporationStructure.Branch AS Expr1, Customer.Delivery.CompanyCode AS Expr12, Customer.Delivery.Branch AS Expr13, 
                      Customer.Delivery.Account AS Expr14, Customer.Delivery.IsActive AS Expr15, Customer.Delivery.DegreeDayBase, Customer.Delivery.DeliveryCode, 
                      Customer.Delivery.UseCode, Customer.Delivery.PostcardInOutFlag, Customer.Delivery.Driver, Customer.Delivery.Route, Customer.Delivery.RouteSequence, 
                      Customer.Delivery.PreviousPercent, Customer.Delivery.PercentFull, Customer.Delivery.TankSerialNumber, Customer.Delivery.TankType, Customer.Delivery.TankUnit,
                       Customer.Delivery.TicketInOutFlag, Customer.Delivery.LockUseOrRateCode, Customer.Delivery.QuantityLastDelivered, Customer.Delivery.TankSize, 
                      Customer.Delivery.NumberOfTanks, Customer.Delivery.NumberOfCompanyTanks, Customer.Delivery.GallonsPerDegreeDay, Customer.Delivery.GallonsPerDay, 
                      Customer.Delivery.HeatingConfidence, Customer.Delivery.DailyConfidence, Customer.Delivery.LastDeliveryDate, Customer.Delivery.LastDeliveryDegreeDay, 
                      Customer.Delivery.OptimumDeliveryDateOrDegreeDay, Customer.Delivery.RunOutDeliveryDateOrDegreeDay, Customer.Delivery.GasCheck, 
                      Customer.Delivery.YearToDateDeliveries, Customer.Delivery.YearToDateGasDelivered, Customer.Delivery.TotalGasUsedLastYear, Customer.Delivery.AXGasUsed1, 
                      Customer.Delivery.AXGasUsed2, Customer.Delivery.AXGasUsed3, Customer.Delivery.AXGasUsed4, Customer.Delivery.AXGasUsed5, 
                      Customer.Delivery.AXDailyUsed1, Customer.Delivery.AXDailyUsed2, Customer.Delivery.AXDailyUsed3, Customer.Delivery.AXDailyUsed4, 
                      Customer.Delivery.AXDailyUsed5, Customer.Delivery.AXDays1, Customer.Delivery.AXDays2, Customer.Delivery.AXDays3, Customer.Delivery.AXDays4, 
                      Customer.Delivery.AXDays5, Customer.Miscellaneous.IsActive AS Expr16, Customer.Miscellaneous.CompanyCode AS Expr17, 
                      Customer.Miscellaneous.Branch AS Expr18, Customer.Miscellaneous.Account AS Expr19, Customer.Miscellaneous.CareOf, Customer.Miscellaneous.DeliveryStreet, 
                      Customer.Miscellaneous.DeliveryCity, Customer.Miscellaneous.PurchaseOrderNumber, Customer.Miscellaneous.MasterBillingBranch, 
                      Customer.Miscellaneous.MasterBillingAccount, Customer.Account.AccountNumber AS Expr2
FROM         Customer.Account LEFT OUTER JOIN
                      Common.Address ON Customer.Account.AccountId = Common.Address.ForeignId LEFT OUTER JOIN
                      Company.CorporationStructure ON Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId AND 
                      Customer.Account.CorporationStructureId = Company.CorporationStructure.CorporationStructureId LEFT OUTER JOIN
                      Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch LEFT OUTER JOIN
                      Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch LEFT OUTER JOIN
                      Customer.Miscellaneous ON Company.CorporationStructure.Branch = Customer.Miscellaneous.Branch LEFT OUTER JOIN
                      Customer.Miscellaneous2 ON Company.CorporationStructure.Branch = Customer.Miscellaneous2.Branch

现在我得到了一个结果,但现在我一遍又一遍地接待同一个客户:

enter image description here

1 个答案:

答案 0 :(得分:1)

原因可能是因为公司结构的双重加入。问题在于:

INNER JOIN
    Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch 
 INNER JOIN
    Customer.Delivery ON Company.CorporationStructure.Branch = Customer.Delivery.Branch 

您需要在from子句中出现两次CorporateStructure,一次针对客户,一次针对交付。