Import data into MATLAB workspace from execution of SQL statement
customizes options for importing data from an executed SQL query by using the
results
= fetch(conn
,sqlquery
,opts
)SQLImportOptions
object.
specifies additional options using one or more name-value pair arguments and any
of the previous input argument combinations. For example,
results
= fetch(___,Name,Value
)'MaxRows',5
imports five rows of data.
specifies additional options using one or more name-value pair arguments. For
example, results
= fetch(conn
,pstmt
,Name,Value
)'DataReturnFormat','structure'
imports data as a
structure.
The name-value pair argument 'MaxRows'
has these
limitations:
If you are using Microsoft® Access™, the native ODBC interface is not supported.
Not all database drivers support setting the maximum number of rows before query execution. For an unsupported driver, modify your SQL query to limit the maximum number of rows to return. The SQL syntax varies with the driver. For details, consult the driver documentation.
The name-value pair argument 'VariableNamingRule'
has these
limitations:
The fetch
function returns an error when you
specify the 'VariableNamingRule'
name-value pair argument and
set the 'DataReturnFormat'
name-value pair argument to
cellarray
, structure
, or
numeric
.
The fetch
function returns a warning when you set
the VariableNamingRule
property of the SQLImportOptions
object to "preserve"
and set
the 'DataReturnFormat'
name-value pair argument to
structure
.
The fetch
function returns an error when you use
the 'VariableNamingRule'
name-value pair argument with the
SQLImportOptions
object opts
.
When the 'VariableNamingRule'
name-value pair argument is
set to the value 'modify'
:
These variable names are reserved identifiers for the
table
data type: Properties
,
RowNames
, and
VariableNames
.
The length of each variable name must be less than the number returned
by namelengthmax
.
The order of records in your database does not remain constant. Sort data
using the SQL ORDER BY
command in your
sqlquery
statement.
For Microsoft
Excel®, tables in sqlquery
are Excel worksheets. By default, some worksheet names include a
$
symbol. To select data from a worksheet with this name
format, use an SQL statement of the form SELECT * FROM
"Sheet1$
" (or 'Sheet1$'
).
Before you modify database tables, ensure that the database is not open for editing. If you try to edit the database while it is open, you receive this MATLAB error:
[Vendor][ODBC Driver] The database engine could not lock table 'TableName' because it is already in use by another person or process.
The PostgreSQL database management system supports multidimensional fields,
but SQL SELECT
statements fail when retrieving these fields
unless you specify an index.
Some databases require that you include a symbol, such as
#
, before and after a date in a query, as follows:
execute(conn,'SELECT * FROM mydb WHERE mydate > #03/05/2005#')
Executing the fetch
function with the
opts
input argument and the
'DataReturnFormat'
name-value pair argument set to the
'numeric'
value has no effect. A corresponding warning
message appears in the Command Window.
The fetch
function imports data using the command line. To
import data interactively, use the Database Explorer app.
bindParamValues
| close
| close
| database
| databaseImportOptions
| databasePreparedStatement
| execute
| getoptions
| reset
| setoptions