List order items for all orders
Read order items for all matching orders in one query. This can be slower than fetching items per order, especially for large date ranges.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
SELECT * FROM OrderItems
WITH(
CreatedAfter='1900-01-01T00:00:00'
-- , CreatedBefore='1900-01-01T00:00:00'
-- , LastUpdatedAfter='1900-01-01T00:00:00'
-- , LastUpdatedBefore='1900-01-01T00:00:00'
-- , OrderStatuses='Pending~Unshipped~PartiallyShipped~PendingAvailability~Shipped~Canceled~Unfulfillable'
-- , MarketplaceIds='ATVPDKIKX0DER~A2Q3Y263D00KWC~A2EUQ1WTGCTBG2'
-- , FulfillmentChannels='AFN~MFN'
-- , PaymentMethods='COD~CVS~Other'
-- , AmazonOrderIds='1111111,222222,333333'
)
--CONNECTION(
-- ServiceUrl='https://sellingpartnerapi-na.amazon.com'
--)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_AMAZON_SELLING_PARTNER_SP_API_IN_GATEWAY], 'SELECT * FROM OrderItems
WITH(
CreatedAfter=''1900-01-01T00:00:00''
-- , CreatedBefore=''1900-01-01T00:00:00''
-- , LastUpdatedAfter=''1900-01-01T00:00:00''
-- , LastUpdatedBefore=''1900-01-01T00:00:00''
-- , OrderStatuses=''Pending~Unshipped~PartiallyShipped~PendingAvailability~Shipped~Canceled~Unfulfillable''
-- , MarketplaceIds=''ATVPDKIKX0DER~A2Q3Y263D00KWC~A2EUQ1WTGCTBG2''
-- , FulfillmentChannels=''AFN~MFN''
-- , PaymentMethods=''COD~CVS~Other''
-- , AmazonOrderIds=''1111111,222222,333333''
)
--CONNECTION(
-- ServiceUrl=''https://sellingpartnerapi-na.amazon.com''
--)')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_AMAZON_SELLING_PARTNER_SP_API_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM OrderItems
WITH(
CreatedAfter=''1900-01-01T00:00:00''
-- , CreatedBefore=''1900-01-01T00:00:00''
-- , LastUpdatedAfter=''1900-01-01T00:00:00''
-- , LastUpdatedBefore=''1900-01-01T00:00:00''
-- , OrderStatuses=''Pending~Unshipped~PartiallyShipped~PendingAvailability~Shipped~Canceled~Unfulfillable''
-- , MarketplaceIds=''ATVPDKIKX0DER~A2Q3Y263D00KWC~A2EUQ1WTGCTBG2''
-- , FulfillmentChannels=''AFN~MFN''
-- , PaymentMethods=''COD~CVS~Other''
-- , AmazonOrderIds=''1111111,222222,333333''
)
--CONNECTION(
-- ServiceUrl=''https://sellingpartnerapi-na.amazon.com''
--)'
EXEC (@MyQuery) AT [LS_TO_AMAZON_SELLING_PARTNER_SP_API_IN_GATEWAY]