How do you specify Excel columns with spaces when using OpenRowSet?

  • I'm trying to add data to the first four columns (of 10) to an excel spreadsheet through the use of OpenRowSet.

    Normally, you would do this like:

    INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\Test.XLS', 'select {columnlist} from [Sheet1$]')

    However, some of those columns in Excel have spaces in their name.

    How do you specify those columns in the {columnlist}?

    The only way I've been able to figure out is to use the Hdr=No and to specify the columns as F1,F2, etc.

    INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\Test.XLS;Hdr=No', 'select F1,F2,F3,F4 from [Sheet1$]')

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • put them in brackets []

    [column one], [column two]



    Pradeep Singh

  • Thanks. I had tried this, and it didn't work. But it's working now for me.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

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

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