To import data from an ODBC Data Source, you'll need 2 things:
1. Enterprise Edition of EViews 5 (or later).
2. An ODBC DSN (Database Source Name) defined on the same system that EViews is on. This DSN should point to the database you'd like to read from and specifies the database server, the user to login as, and possibly the default database (if applicable) to connect to.
Once you have your DSN ready, you can start the read process by:
1. In EViews, go to File->Open->Foreign Data as Workfile.
2. Select "ODBC Database..." from the Files of Type dropdown (should be at the bottom of the dropdown).
3. Select your DSN. If you don't have one yet, you can create one by clicking on the New button. Click OK.
4. On the "Select Table" popup, specify the table to import or a query. Click OK.
5. On the "Table read specification" popup, specify the columns to import. Click OK.
Once complete, you should have a new workfile that contains a series for each column imported.
To do this programmatically, you can use either 'wfopen' or 'pageload' to load in the data. 'wfopen' is used to read data into a new workfile where as 'pageload' is used to read data into a new page in the current workfile. Examples of how to use these two methods follow:
1.
Load entire table into EViews:
Code: Select all
wfopen(type=odbc) dsn tablename @keep *
pageload(type=odbc) dsn tablename @keep *
This will load every column found in the table into a new workfile.
NOTE: The name of the new workfile (and/or page) will match the dsn by default. To specify a different workfile name (and/or page name) use the following:
Code: Select all
wfopen(type=odbc, wf=newname, page=newpagename) dsn tablename @keep *
pageload(type=odbc, page=newpagename) dsn tablename @keep *
2.
Load only certain columns from a table:
Code: Select all
wfopen(type=odbc) dsn tablename @keep column1 column2 h*
pageload(type=odbc) dsn tablename @keep column1 column2 h*
This will load column1, column2, and all columns whose name starts with 'h'.
3.
Load only certain columns and certain rows from a table:
Code: Select all
wfopen(type=odbc) dsn tablename @keep column1 column2 h* @selectif column1<10
pageload(type=odbc) dsn tablename @keep column1 column2 h* @selectif column1<10
This will load column1, column2, and all columns whose name starts with 'h', but only those rows where column1 has a numeric value less than 10.
NOTE: Though this method will work, it is not the best method to filter rows. The better method will be to use a query with a where condition instead of a tablename (see #4).
4.
Read data from a query:
Code: Select all
wfopen(type=odbc) dsn "query string" @keep *
pageload(type=odbc) dsn "query string" @keep *
This will load all columns from the resultset returned by the specified query. The query can specify which columns to return and any specific where conditions.
NOTE: EViews, by default, will wait up to 30 seconds for the database to start returning the data. After that, a timeout error will be displayed. If the query needs more time to execute, you can specify a higher timeout value:
Code: Select all
wfopen(type=odbc, timeout=secs) dsn "query string"
pageload(type=odbc, timeout=secs) dsn "query string"
Also, the @keep and the @selectif property can still be used to further restrict which columns and rows to actually import. When running this command in EViews interactive mode, not specifying @keep will result in the "Specify Table" popup being displayed to allow you to choose which columns to import.
Here's an example of an import using a query string with a timeout value:
Code: Select all
wfopen(type=odbc, timeout=60) mydb "select column1, column2 from mytable where column1 < 10" @keep *
pageload(type=odbc, timeout=60) mydb "select * from mytable where column1 < 10" @keep *
5.
Read data from a stored procedure:
Code: Select all
wfopen(type=odbc) dsn "{call spName}" @keep *
pageload(type=odbc) dsn "{call spName}" @keep *
This will load all columns from the resultset returned by the specified stored procedure. If the stored procedure takes in parameters, you can specify them like this:
Code: Select all
wfopen(type=odbc) mydb "{call GetCustomData(5, 'custom text')}" @keep *
pageload(type=odbc) mydb "{call GetCustomData(5, 'custom text')}" @keep *
Steve