Ordering Issue while importing excel file from excel to sql server table with Openrowset

Ranjan Kalita 0 Reputation points
2023-09-05T10:27:35.4933333+00:00

Issue Description

We are using OPENROWSET to load data from excel file to SQL Server2019 Table.

select *  into dbo.StagingTest

FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\PDTS\Test\Test.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')

 

select * from dbo.StagingTest

 

--drop table dbo.StagingTest

Data got staged to dbo.StagingTest but we can see data does not retain its order as header row moved from first to nth row in staging table. The issue is intermittent and can’t be reproduced in lower environment.

Issue usually happen with large file. File having around 37000 rows that errored out.

Excel:

User's image

Staging Table:

User's image

 

Can you please help why this happen and is there any resolution?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,491 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 46,281 Reputation points
    2023-09-05T11:47:18.4966667+00:00

    but we can see data does not retain its order

    Data don't have a "natural order", it always can be some random.

    0 comments No comments

  2. Yitzhak Khabinsky 26,471 Reputation points
    2023-09-05T12:39:55.81+00:00

    Hi @Ranjan Kalita,

    You can try to modify your statement as follows:

    SELECT * INTO dbo.StagingTest
    FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
    	'Excel 12.0 Xml; HDR=YES;
    	Database=D:\PDTS\Test\Test.xlsx', 
    	'[Sheet1$]');
    
    
    

    Explanation:

    File Type (extension) Extended Properties


    Excel 97-2003 Workbook (.xls) "Excel 8.0"

    Excel Workbook (.xlsx) "Excel 12.0 Xml"

    Excel Macro-enabled workbook (.xlsm) "Excel 12.0 Macro"

    Excel Non-XML binary workbook (.xlsb) "Excel 12.0"

    "HDR=Yes;" indicates that the first row contains column names, not data. "HDR=No;" indicates the opposite.

    "HDR=Yes;" will omit (ignore) the first row while getting data from an Excel file.

    For the reference: https://www.connectionstrings.com/ace-oledb-12-0/

    0 comments No comments

  3. Erland Sommarskog 119.7K Reputation points MVP
    2023-09-05T21:33:12.7266667+00:00

    There are not first or last rows in a table. Tables are by definition unordered sets. The only way to assign "order" to data in a table is by the data itself. If you want to look at the data with the header rows on top, you need to do:

    SELECT *
    FROM  tbl
    ORDER BY CASE F1 = 'Personalnumber THEN 1 ELSE 2 END
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.