I have a .CSV
file containing thousands of SKUs. I would like to lookup the available quantities in my database for only the SKUs listed within this .CSV
file.
Here's what I have so far: (Let's say a couple SKUs are named "ABC" and "XYZ")
The SQL (on SQL Server 2008 R2):
SELECT
[sku], [qty]
FROM
[Inventory]
WHERE
[sku] IN (SELECT skuColumn
FROM OPENROWSET(BULK 'C:\skus.csv',
FORMATFILE='C:\skusFormat.xml') AS data)
The CSV file (skus.csv):
ABC
XYZ
...
The XML format file (skusFormat.xml):
<?xml version="1.0" ?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\n" MAX_LENGTH="40" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="sku" xsi:type="SQLNVARCHAR" />
</ROW>
</BCPFORMAT>
When I run the SELECT... OPENROWSET()
statement on its own, a single column of SKUs is returned - as expected; however, when I use it as a subquery within IN()
the results are empty.
I'm expecting the results to be the SKUs from the skus.csv
file and their quantities from the Inventory
database. What am I doing wrong here?
Thanks!
could be data type mismatched
and using EXISTS instead of IN
SELECT [sku], [qty]
FROM [Inventory] x
WHERE EXISTS (
SELECT 1 FROM OPENROWSET(
BULK 'C:\skus.csv',
FORMATFILE='C:\skusFormat.xml'
) AS data
where cast(data.skuColumn as char(10)) = x.sku
)
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.