Read Excel from DB2

2019-08-20 03:10发布

问题:

I have to import some Excel data on a regular basis. Checking the DB2 documentation one can directly access OLE DB datasources via an external function.

However I'm unable to set it up properly. I got the Microsoft Access Database Enginge 2010 plus the fix pack and installed it on the database server.

I placed the excel file in a local directory from the database server. (C:\Temp\test.xls)

The excel has a workbook called TEST1 and two rows ABC and DEF following some numeric data:

ABC | DEF
---------
1   | 5
2   | 6
3   | 7
4   | 8

For creating the table function I used the following statement:

CREATE OR REPLACE FUNCTION MYSCHEMA.test_excel ()
  RETURNS TABLE(ABC INTEGER,
            DEF INTEGER)
LANGUAGE OLEDB
EXTERNAL NAME '!TEST1!Provider=Microsoft.ACE.OLEDB.12.0;
      Data Source=C:\Temp\test.xls;
  Extended Properties="Excel 8.0;HDR=Yes"';

It seems to create that function. However when querying the data with:

SELECT * FROM TABLE(MYSCHEMA.test_excel()) AS FUNCTABLE;

I'm getting the following error:

User defined function "MYSCHEMA.TEST_EXCEL" received an OLE DB error from specified OLE DB provider. HRESULT="0x80040e37". Diagnostic text: "The Microsoft Access database engine".. SQLCODE=-1183, SQLSTATE=38506, DRIVER=3.53.71

According to the documentation the error means: 0x80040E37 The specified table does not exist.

The !TEST! should reference the workbook, however I'm unsure if it is correct syntax. How can one access a Excel worksheet from DB2? Is there a way to get a more detailed error message? Does anyone the correct naming scheme?

回答1:

The naming seems to be incorrect. Looking at other oledb samples I figured a '$' was missing.

!TEST$! works as an external name when referencing a worksheet called TEST and I can access the data.