简体   繁体   中英

MSSQL UNION var types mismatch

Question asked before and I believe I did all necessary conversions, yet I'm getting error: Error converting data type nvarchar to float . The error message is referencing to SnapperMusic_Winman - View1

View1:

SELECT     Products.ProductId AS Product, Products.ProductDescription AS Description, CONVERT(datetime, PurchaseOrders.EffectiveDate) AS OrderDate, 
                      Suppliers.SupplierId AS SupplierAcc, Suppliers.SupplierName, CONVERT(datetime, Inventory.EffectiveDate) AS BookedDate, CONVERT(float, Inventory.Quantity) 
                      AS Quantity, CONVERT(float, Inventory.TotalValue) AS TotalValue, Locations.LocationId AS Location, PurchaseOrders.PurchaseOrderId AS PONumber, 
                      GoodsReceipts.GoodsReceiptId AS GoodsReference
FROM         SnapperMusic_Winman.dbo.GoodsReceipts AS GoodsReceipts INNER JOIN
                      SnapperMusic_Winman.dbo.Inventory AS Inventory ON GoodsReceipts.GoodsReceipt = Inventory.GoodsReceipt INNER JOIN
                      SnapperMusic_Winman.dbo.Suppliers AS Suppliers ON GoodsReceipts.Supplier = Suppliers.Supplier INNER JOIN
                      SnapperMusic_Winman.dbo.Products AS Products ON Inventory.Product = Products.Product INNER JOIN
                      SnapperMusic_Winman.dbo.Locations AS Locations ON Inventory.Location = Locations.Location INNER JOIN
                      SnapperMusic_Winman.dbo.PurchaseOrderItems AS PurchaseOrderItems ON Inventory.PurchaseOrderItem = PurchaseOrderItems.PurchaseOrderItem INNER JOIN
                      SnapperMusic_Winman.dbo.PurchaseOrders AS PurchaseOrders ON PurchaseOrderItems.PurchaseOrder = PurchaseOrders.PurchaseOrder

View2:

SELECT     GoodsReceipts.ItemCode AS Product, dbo.StockItem.Name AS Description, CONVERT(datetime, dbo.POPOrderReturn.DocumentDate) AS OrderDate, 
                      dbo.PLSupplierAccount.SupplierAccountNumber AS SupplierAcc, dbo.PLSupplierAccount.SupplierAccountName AS SupplierName, CONVERT(datetime, 
                      dbo.POPReceiptReturnLine.DateTimeCreated) AS BookedDate, CONVERT(float, dbo.POPReceiptReturnLine.ReceiptReturnQuantity) AS Quantity, CONVERT(float, 
                      GoodsReceipts.UnitBuyingPrice * dbo.POPReceiptReturnLine.ReceiptReturnQuantity) AS TotalValue, dbo.Warehouse.Name AS Location, 
                      dbo.POPOrderReturn.DocumentNo AS PONumber, dbo.POPInvoiceCreditLine.POPInvoiceCreditNo AS GoodsReference
FROM         dbo.POPOrderReturnLine AS GoodsReceipts INNER JOIN
                      dbo.POPReceiptReturnLine ON GoodsReceipts.POPOrderReturnLineID = dbo.POPReceiptReturnLine.POPOrderReturnLineID INNER JOIN
                      dbo.POPOrderReturn ON GoodsReceipts.POPOrderReturnID = dbo.POPOrderReturn.POPOrderReturnID INNER JOIN
                      dbo.PLSupplierAccount ON dbo.POPOrderReturn.SupplierID = dbo.PLSupplierAccount.PLSupplierAccountID INNER JOIN
                      dbo.Warehouse ON dbo.POPOrderReturn.WarehouseID = dbo.Warehouse.WarehouseID INNER JOIN
                      dbo.POPInvoiceCreditLine ON GoodsReceipts.POPOrderReturnLineID = dbo.POPInvoiceCreditLine.POPOrderReturnLineID INNER JOIN
                      dbo.StockItem ON GoodsReceipts.ItemCode = dbo.StockItem.Code

Union:

SELECT     dbo.Elen_GoodsInReceived_Winman.Product, dbo.Elen_GoodsInReceived_Winman.Description, dbo.Elen_GoodsInReceived_Winman.OrderDate, 
                      dbo.Elen_GoodsInReceived_Winman.SupplierAcc, dbo.Elen_GoodsInReceived_Winman.SupplierName, dbo.Elen_GoodsInReceived_Winman.BookedDate, 
                      dbo.Elen_GoodsInReceived_Winman.Quantity, dbo.Elen_GoodsInReceived_Winman.Location, dbo.Elen_GoodsInReceived_Winman.TotalValue, 
                      dbo.Elen_GoodsInReceived_Winman.PONumber, dbo.Elen_GoodsInReceived_Winman.GoodsReference
FROM         dbo.Elen_GoodsInReceived_Winman
UNION
SELECT    dbo.Elen_GoodsInReceived_Sage.Product, dbo.Elen_GoodsInReceived_Sage.Description, dbo.Elen_GoodsInReceived_Sage.OrderDate, 
                      dbo.Elen_GoodsInReceived_Sage.SupplierAcc , dbo.Elen_GoodsInReceived_Sage.SupplierName, 
                      dbo.Elen_GoodsInReceived_Sage.BookedDate, dbo.Elen_GoodsInReceived_Sage.Quantity, 
                      dbo.Elen_GoodsInReceived_Sage.TotalValue, dbo.Elen_GoodsInReceived_Sage.Location, 
                      dbo.Elen_GoodsInReceived_Sage.PONumber, dbo.Elen_GoodsInReceived_Sage.GoodsReference
FROM         dbo.Elen_GoodsInReceived_Sage

Apart from converted fields all others are VARCHAR . Needless to say that individually they work fine..

Can anyone point out what am I missing please?

You have the order of columns in the select statements wrong at one point.

In view1 it is

   dbo.Elen_GoodsInReceived_Winman.Location,     
   dbo.Elen_GoodsInReceived_Winman.TotalValue,

In view2 it is

 dbo.Elen_GoodsInReceived_Sage.TotalValue, 
 dbo.Elen_GoodsInReceived_Sage.Location, 

Change this order to be appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM