The company I am working for is testing an inventory platform for their warehouse. I need to test this with them and it requires an import with customer information billing/shipping and order details. So I pulled this sql script together and its giving me trouble. Since there isnt an available export with all the fields i needed- and I couldnt find a table index (probably for good reason) I used the save columns feature which writes a sql query. Then went from that point and constructed this.

Im not a SQL expert by any stretch- I get the errors Your SQL is invalid: Incorrect syntax near the keyword 'FROM'.Your SQL is invalid: Incorrect syntax near 'WHERE'.

Below is the query code:

select
o.LastModified,
o.LastModBy,
o.OrderStatus,
o.OrderID,
o.OrderDate,
o.ShipDate,
o.PaymentAmount,
o.Total_Payment_Authorized,
o.Total_Payment_Received,
o.CustomerID,
o.Locked,
o.Printed,
o.CreditCardAuthorizationDate,
o.Shipped,
o.PaymentDeclined,
o.SalesTaxRate1,
o.SalesTaxRate2,
o.SalesTaxRate3,
o.SalesTax1,
o.SalesTax2,
o.SalesTax3,
o.Tax1_Title,
o.Tax2_Title,
o.Tax3_Title,
o.TotalShippingCost,
o.BatchNumber,
o.Order_Entry_System,
o.SalesRep_CustomerID,
o.Customer_IPAddress,
o.Custom_Field_Custom1,
o.Custom_Field_Custom2,
o.Custom_Field_Custom3,
o.Custom_Field_Custom4,
o.Custom_Field_Custom5 ,

(SELECT Customers.EmailAddress FROM Customers WITH (NOLOCK) WHERE Customers.CustomerID = o.CustomerID) As EmailAddress ,
o.ShipCompanyName,
o.ShipFirstName,
o.ShipLastName,
o.ShipAddress1,
o.ShipAddress2,
o.ShipCity,
o.ShipState,
o.ShipPostalCode,
o.ShipCountry,
o.ShipPhoneNumber,
o.ShipFaxNumber,
o.ShipResidential,
o.ShippingMethodID ,
o.BillingCompanyName,
o.BillingFirstName,
o.BillingLastName,
o.BillingAddress1,
o.BillingAddress2,
o.BillingCity,
o.BillingState,
o.BillingPostalCode,
o.BillingCountry,
o.BillingPhoneNumber,
o.BillingFaxNumber ,
Fraud.Score ,

(SELECT PaymentMethod FROM PaymentMethods WITH (NOLOCK) WHERE PaymentMethods.PaymentMethodID = o.PaymentMethodID) As PaymentMethod, IsNull(OrderDetails.Quantity,0) as Quantity, IsNull(OrderDetails.QtyOnBackOrder,0) as QtyOnBackOrder, IsNull(OrderDetails.QtyOnHold,0) as QtyOnHold, IsNull(OrderDetails.QtyShipped,0) as QtyShipped, (SELECT TOP 1 Pay_AVS_Response FROM Payment_Log WITH (NOLOCK) WHERE Pay_OrderID = o.OrderID AND IsNull(Deleted,'N') <> 'Y' AND (Pay_Result = 'AUTHORIZE' OR Pay_Result = 'DEBIT')) AS Payment_Log_AVS_Response, (SELECT TOP 1 Pay_CVV2_Response FROM Payment_Log WITH (NOLOCK) WHERE Pay_OrderID = o.OrderID AND IsNull(Deleted,'N') <> 'Y' AND (Pay_Result = 'AUTHORIZE' OR Pay_Result = 'DEBIT')) AS Payment_Log_CVV2_Response FROM Orders o WITH (NOLOCK) LEFT OUTER JOIN vMerchant.Fraud WITH (NOLOCK) ON Fraud.OrderID = o.OrderID LEFT JOIN ( SELECT OrderID, Sum(Quantity) As Quantity, Sum(QtyOnBackOrder) As QtyOnBackOrder, Sum(QtyOnHold) As QtyOnHold, Sum(QtyShipped) As QtyShipped, FROM OrderDetails WITH (NOLOCK) GROUP BY OrderID ) OrderDetails ON o.OrderID = OrderDetails.OrderID WHERE 1=1