Bulk Insert From CSV using OPENROWSET

  • INSERT INTO chief_product (name, description, price, sale_price, vendor_number, vendor_name, product_code, sortorder, type, showitem, parentid, children, new_date)

    SELECT name, description, price, sale_price, vendor_number, vendor_name, product_code

    FROM OPENROWSET(BULK '\\path\bulk.csv', FORMATFILE='\\path\bulk_format.Xml', FIRSTROW = 2) as t1, 'productchild', 'no', 'parentID', 0, GETDATE()

    ERROR:

    "Incorrect syntax near 'productchild'."

    Only the first 7 of the 13 columns I'm trying to insert come from the *.CSV

    How can I insert the remaining 6 columns?

    Everything worked fine when I was just inserting the first 7 columns...again the question is how to input the remaining 6 columns.

    Thanks in advance!

  • Sorry admin...I tried to delete my duplicate posts. But, I got an error. The reason there were duplicate posts is b/c I got an error every time I posted saying that the post didn't go through. I didn't realize they actually posted until I went back to the from.

    My appologies. If you could remove the duplicates and just leave the last one that'd be great!

    Thanks in advance!

  • Figured it out...read my *.CSV into a recordset using OPENROWSET

    Then I loop through that recordset inserting each row of CSV data along with the 6 other columns of data. I'll post the code when I'm done.

  • INSERT INTO chief_product (name, description, price, sale_price, vendor_number, vendor_name, product_code, sortorder, type, showitem, parentid, children, new_date)

    SELECT *

    FROM OPENROWSET(BULK '\\path\bulk.csv', FORMATFILE='\\path\bulk_format.Xml', FIRSTROW = 2) as t1

    inner join

    (select 'productchild' as productchild, 'no' as , 'parentID' as , 0 as , GETDATE() as ) T2

    on t1.priceT2.productchild

    note that your list of column is wrong in select clause.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply