从MSSQL到MySQL创建视图时出现语法错误

时间:2014-11-25 01:57:20

标签: mysql sql sql-server views union-all

我在SQL上创建了这个视图脚本:

Create View dbo.vwinventorysource
As
(
SELECT     'Bills' AS SourceName, Bills.BranchNo, Branches.BranchName, Bills.BillDate AS TranDate, Bills.SalesInvoiceNo AS RefDoc, Products.ProductCatNo, 
                      ProductCategories.ProdCatName, BillDetails.ProductNo, Products.ProductName, Products.BrandName, Products.ReorderPoint, BillDetails.LotNo, 
                      BillDetails.ExpiryDate, BillDetails.CostPerQty, CASE WHEN Products.ActiveMarkup = 1 THEN BillDetails.CostPerQty * (1 + (Products.Markup1 / 100.00)) 
                      ELSE CASE WHEN Products.ActiveMarkup = 2 THEN BillDetails.CostPerQty * (1 + (Products.Markup2 / 100.00)) 
                      ELSE BillDetails.CostPerQty * (1 + (Products.Markup3 / 100.00)) END END SellingPrice, Products.WholeSaleUnit, 
                      (CASE WHEN BillDetails.Unit = Products.RetailUnit THEN BillDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE BillDetails.Qty END + CASE WHEN BillDetails.FreebiesUnit
                       = Products.RetailUnit THEN BillDetails.FreebiesQty / Products.RetailQtyPerWholeSaleUnit ELSE BillDetails.FreebiesQty END) AS WholeSaleQty, Products.RetailUnit, 
                      (CASE WHEN BillDetails.Unit = Products.WholeSaleUnit THEN BillDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE BillDetails.Qty END + CASE WHEN BillDetails.FreebiesUnit
                       = Products.WholeSaleUnit THEN BillDetails.FreebiesQty * Products.RetailQtyPerWholeSaleUnit ELSE BillDetails.FreebiesQty END) AS RetailSaleQty
FROM         Bills INNER JOIN
                      BillDetails ON Bills.Id = BillDetails.BillNo INNER JOIN
                      Products ON BillDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON Bills.BranchNo = Branches.Id
WHERE     Bills.ApprovedBy <> '' AND Bills.IsCancelled = 'N'
UNION ALL
SELECT     'SI' AS SourceName, SalesInvoices.BranchNo, Branches.BranchName, SalesInvoices.InvoiceDate AS TranDate, SalesInvoices.SalesInvoiceRefDocNo AS RefDoc, 
                      Products.ProductCatNo, ProductCategories.ProdCatName, SalesInvoiceDetails.ProductNo, Products.ProductName, Products.BrandName, Products.ReorderPoint, 
                      SalesInvoiceDetails.LotNo, SalesInvoiceDetails.ExpiryDate, 0.00 AS CostPerQty, SalesInvoiceDetails.UnitPrice AS SellingPrice, Products.WholeSaleUnit, 
                      ((CASE WHEN SalesInvoiceDetails.Unit = Products.RetailUnit THEN SalesInvoiceDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE SalesInvoiceDetails.Qty END +
                       CASE WHEN SalesInvoiceDetails.FreebiesUnit = Products.RetailUnit THEN SalesInvoiceDetails.FreebiesQty / Products.RetailQtyPerWholeSaleUnit ELSE SalesInvoiceDetails.FreebiesQty
                       END) * - 1) AS WholeSaleQty, Products.RetailUnit, 
                      ((CASE WHEN SalesInvoiceDetails.Unit = Products.WholeSaleUnit THEN SalesInvoiceDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE SalesInvoiceDetails.Qty
                       END + CASE WHEN SalesInvoiceDetails.FreebiesUnit = Products.WholeSaleUnit THEN SalesInvoiceDetails.FreebiesQty * Products.RetailQtyPerWholeSaleUnit ELSE SalesInvoiceDetails.FreebiesQty
                       END) * - 1) AS RetailSaleQty
FROM         SalesInvoices INNER JOIN
                      SalesInvoiceDetails ON SalesInvoices.Id = SalesInvoiceDetails.SalesInvoiceNo INNER JOIN
                      Products ON SalesInvoiceDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON SalesInvoices.BranchNo = Branches.Id
WHERE     SalesInvoices.IsCancelled = 'N'
UNION ALL
SELECT     'SupplierReturns ' AS SourceName, SupplierReturns.BranchNo, Branches.BranchName, SupplierReturns.ReturnDate AS TranDate, Bills.SalesInvoiceNo AS RefDoc, 
                      Products.ProductCatNo, ProductCategories.ProdCatName, Products.Id, Products.ProductName, Products.BrandName, Products.ReorderPoint, 
                      SupplierReturnDetails.LotNo, SupplierReturnDetails.ExpiryDate, SupplierReturnDetails.CostPerQty, 0.00 AS SellingPrice, Products.WholeSaleUnit, 
                      ((CASE WHEN SupplierReturnDetails.Unit = Products.RetailUnit THEN SupplierReturnDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE SupplierReturnDetails.Qty
                       END + CASE WHEN SupplierReturnDetails.FreebiesUnit = Products.RetailUnit THEN SupplierReturnDetails.FreebiesQty / Products.RetailQtyPerWholeSaleUnit ELSE SupplierReturnDetails.FreebiesQty
                       END) * - 1) AS WholeSaleQty, Products.RetailUnit, 
                      ((CASE WHEN SupplierReturnDetails.Unit = Products.WholeSaleUnit THEN SupplierReturnDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE SupplierReturnDetails.Qty
                       END + CASE WHEN SupplierReturnDetails.FreebiesUnit = Products.WholeSaleUnit THEN SupplierReturnDetails.FreebiesQty * Products.RetailQtyPerWholeSaleUnit ELSE
                       SupplierReturnDetails.FreebiesQty END) * - 1) AS RetailSaleQty
FROM         SupplierReturns INNER JOIN
                      SupplierReturnDetails ON SupplierReturns.Id = SupplierReturnDetails.SupplierReturnNo INNER JOIN
                      Products ON SupplierReturnDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Bills ON SupplierReturns.BillNo = Bills.Id INNER JOIN
                      Branches ON SupplierReturns.BranchNo = Branches.Id
WHERE     SupplierReturns.ApprovedBy <> '' AND SupplierReturns.IsCancelled = 'N'
UNION ALL
SELECT     'CustomerReturns' AS SourceName, CustomerReturns.BranchNo, Branches.BranchName, CustomerReturns.CustomerReturnDate AS TranDate, 
                      CustomerReturns.SalesinvoiceNo AS RefDoc, Products.ProductCatNo, ProductCategories.ProdCatName, Products.Id, Products.ProductName, Products.BrandName, 
                      Products.ReorderPoint, CustomerReturnDetails.LotNo, CustomerReturnDetails.ExpiryDate, 0.00 CostPerQty, CustomerReturnDetails.UnitPrice AS SellingPrice, 
                      Products.WholeSaleUnit, 
                      (CASE WHEN CustomerReturnDetails.Unit = Products.RetailUnit THEN CustomerReturnDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE CustomerReturnDetails.Qty
                       END + CASE WHEN CustomerReturnDetails.FreebiesUnit = Products.RetailUnit THEN CustomerReturnDetails.FreebiesQty / Products.RetailQtyPerWholeSaleUnit ELSE
                       CustomerReturnDetails.FreebiesQty END) AS WholeSaleQty, Products.RetailUnit, 
                      (CASE WHEN CustomerReturnDetails.Unit = Products.WholeSaleUnit THEN CustomerReturnDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE CustomerReturnDetails.Qty
                       END + CASE WHEN CustomerReturnDetails.FreebiesUnit = Products.WholeSaleUnit THEN CustomerReturnDetails.FreebiesQty * Products.RetailQtyPerWholeSaleUnit ELSE
                       CustomerReturnDetails.FreebiesQty END) AS RetailSaleQty
FROM         CustomerReturns INNER JOIN
                      CustomerReturnDetails ON CustomerReturns.Id = CustomerReturnDetails.CustomerReturnNo INNER JOIN
                      Products ON CustomerReturnDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON CustomerReturns.BranchNo = Branches.Id
WHERE     CustomerReturns.ApprovedBy <> '' AND CustomerReturns.IsCancelled = 'N'
UNION ALL
SELECT     'InvAdjustment' AS SourceName, InventoryAdjustments.BranchNo, Branches.BranchName, InventoryAdjustments.AdjustmentDate AS TranDate, 
                      InventoryAdjustments.Id AS RefDoc, Products.ProductCatNo, ProductCategories.ProdCatName, InventoryAdjustmentDetails.ProductNo, Products.ProductName, 
                      Products.BrandName, Products.ReorderPoint, InventoryAdjustmentDetails.LotNo, InventoryAdjustmentDetails.ExpiryDate, InventoryAdjustmentDetails.CostPerQty, 
                      0.00 AS SellingPrice, Products.WholeSaleUnit, 
                      (CASE WHEN InventoryAdjustmentDetails.Unit = Products.RetailUnit THEN InventoryAdjustmentDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE InventoryAdjustmentDetails.Qty
                       END) AS WholeSaleQty, Products.RetailUnit, 
                      (CASE WHEN InventoryAdjustmentDetails.Unit = Products.WholeSaleUnit THEN InventoryAdjustmentDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE InventoryAdjustmentDetails.Qty
                       END) AS RetailSaleQty
FROM         InventoryAdjustments INNER JOIN
                      InventoryAdjustmentDetails ON InventoryAdjustments.Id = InventoryAdjustmentDetails.InvAdjustmentNo INNER JOIN
                      Products ON InventoryAdjustmentDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON InventoryAdjustments.BranchNo = Branches.Id
WHERE     InventoryAdjustments.ApprovedBy <> '' AND InventoryAdjustments.IsCancelled = 'N'
UNION ALL
SELECT     'InvDamages' AS SourceName, InventoryDamages.BranchNo, Branches.BranchName, InventoryDamages.InvDamageDate AS TranDate, 
                      InventoryDamages.Id AS RefDoc, Products.ProductCatNo, ProductCategories.ProdCatName, InventoryDamageDetails.ProductNo, Products.ProductName, 
                      Products.BrandName, Products.ReorderPoint, InventoryDamageDetails.LotNo, InventoryDamageDetails.ExpiryDate, InventoryDamageDetails.CostPerQty, 
                      0.00 AS SellingPrice, Products.WholeSaleUnit, 
                      ((CASE WHEN InventoryDamageDetails.Unit = Products.RetailUnit THEN InventoryDamageDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE InventoryDamageDetails.Qty
                       END) * - 1) AS WholeSaleQty, Products.RetailUnit, 
                      ((CASE WHEN InventoryDamageDetails.Unit = Products.WholeSaleUnit THEN InventoryDamageDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE InventoryDamageDetails.Qty
                       END) * - 1) AS RetailSaleQty
FROM         InventoryDamages INNER JOIN
                      InventoryDamageDetails ON InventoryDamages.Id = InventoryDamageDetails.InvDamagesNo INNER JOIN
                      Products ON InventoryDamageDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON InventoryDamages.BranchNo = Branches.Id
WHERE     InventoryDamages.ApprovedBy <> '' AND InventoryDamages.IsCancelled = 'N'
UNION ALL
SELECT     'StockTransferOut' AS SourceName, StockTransfers.BranchSourceNo AS BranchNo, Branches.BranchName, StockTransfers.TransferDate AS TranDate, 
                      StockTransfers.Id AS RefDoc, Products.ProductCatNo, ProductCategories.ProdCatName, StockTransferDetails.ProductNo, Products.ProductName, 
                      Products.BrandName, Products.ReorderPoint, StockTransferDetails.LotNo, StockTransferDetails.ExpiryDate, StockTransferDetails.CostPerUnit, 0.00 AS SellingPrice, 
                      Products.WholeSaleUnit, 
                      ((CASE WHEN StockTransferDetails.Unit = Products.RetailUnit THEN StockTransferDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE StockTransferDetails.Qty END)
                       * - 1) AS WholeSaleQty, Products.RetailUnit, 
                      ((CASE WHEN StockTransferDetails.Unit = Products.WholeSaleUnit THEN StockTransferDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE StockTransferDetails.Qty
                       END) * - 1) AS RetailSaleQty
FROM         StockTransfers INNER JOIN
                      StockTransferDetails ON StockTransfers.Id = StockTransferDetails.StockTransferNo INNER JOIN
                      Products ON StockTransferDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON StockTransfers.BranchSourceNo = Branches.Id
WHERE     StockTransfers.ApprovedBy <> '' AND StockTransfers.IsCancelled = 'N'
UNION ALL
SELECT     'StockTransferIn' AS SourceName, StockTransfers.BranchDestinationNo AS BranchNo, Branches.BranchName, StockTransfers.TransferDate AS TranDate, 
                      StockTransfers.Id AS RefDoc, Products.ProductCatNo, ProductCategories.ProdCatName, StockTransferDetails.ProductNo, Products.ProductName, 
                      Products.BrandName, Products.ReorderPoint, StockTransferDetails.LotNo, StockTransferDetails.ExpiryDate, StockTransferDetails.CostPerUnit, 0.00 AS SellingPrice, 
                      Products.WholeSaleUnit, 
                      (CASE WHEN StockTransferDetails.Unit = Products.RetailUnit THEN StockTransferDetails.Qty / Products.RetailQtyPerWholeSaleUnit ELSE StockTransferDetails.Qty END)
                       AS WholeSaleQty, Products.RetailUnit, 
                      (CASE WHEN StockTransferDetails.Unit = Products.WholeSaleUnit THEN StockTransferDetails.Qty * Products.RetailQtyPerWholeSaleUnit ELSE StockTransferDetails.Qty
                       END) AS RetailSaleQty
FROM         StockTransfers INNER JOIN
                      StockTransferDetails ON StockTransfers.Id = StockTransferDetails.StockTransferNo INNER JOIN
                      Products ON StockTransferDetails.ProductNo = Products.Id INNER JOIN
                      ProductCategories ON Products.ProductCatNo = ProductCategories.Id INNER JOIN
                      Branches ON StockTransfers.BranchDestinationNo = Branches.Id
WHERE     StockTransfers.ApprovedBy <> '' AND StockTransfers.IsCancelled = 'N'
)

我在MSSQL上运行它并且运行正常。 但是当我试图在MySQL上运行它时,它给我一个语法错误说

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION ALL
SELECT     'SI' AS SourceName, SalesInvoices.BranchNo, Branches.Branc' at line 19 "

将查询从MSSQL转换为MySQL有什么问题吗?或者是否与UNION ALL运算符有关?感谢您的见解和解决方案。

0 个答案:

没有答案