I need to import data from an Excel sheet to a random table in a database using Excel ace adapter. (http://www.microsoft.com/en-us/download/details.aspx?id=13255)
Here is my query :
SELECT * into xyz FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; HDR=YES; IMEX=1; Database=F:\export.xlsx',
'SELECT * FROM [sheet1$]')
This works fine except that the data in the table is not in the same order as the Excel sheet. Is there any way to ensure the order remains unchanged.
If I can include the row number from the sheet during import, it would be a blessing!
Please let me know
I fixed the issue in my case by using this :
INSERT INTO xyz SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; HDR=YES; IMEX=1; Database=F:\export.xlsx',
'SELECT * FROM [sheet1$]')
Note : xyz had to have an identity field - incremental integer of step count 1
This seemed to force the insert to maintain the same order.
As I knew the number of columns and column names in my excel sheet would remain constant, I was able to use this approach.