fetch
Import data into MATLAB workspace from execution of SQL statement
Syntax
Description
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 arguments with any of
the previous input argument combinations. For example, specify results
= fetch(___,Name,Value
)MaxRows
= 5
to import five rows of data.
specifies additional options using one or more name-value arguments. For
example, specify results
= fetch(conn
,pstmt
,Name,Value
)DataReturnFormat = "structure"
to import
data as a structure.
Examples
Input Arguments
Output Arguments
Limitations
The name-value 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 argument VariableNamingRule
has these
limitations:
The
fetch
function returns an error if you specify theVariableNamingRule
name-value argument and set theDataReturnFormat
name-value argument to"cellarray"
,"structure"
, or"numeric"
.The
fetch
function returns a warning if you set theVariableNamingRule
property of theSQLImportOptions
object to"preserve"
and set theDataReturnFormat
name-value argument to"structure"
.The
fetch
function returns an error if you use theVariableNamingRule
name-value argument with theSQLImportOptions
objectopts
.If you set the
VariableNamingRule
name-value argument to the value"modify"
:These variable names are reserved identifiers for the
table
data type:Properties
,RowNames
, andVariableNames
.The length of each variable name must be less than the number returned by
namelengthmax
.
The name-value argument RowFilter
has this limitation:
The
fetch
function returns an error if you specify theRowFilter
name-value argument with theSQLImportOptions
objectopts
. It is ambiguous which of theRowFilter
object to use in this case, especially if the filter conditions are different.
Tips
The order of records in your database does not remain constant. Sort data using the SQL
ORDER BY
command in yoursqlquery
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 formSELECT * 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 theopts
input argument and theDataReturnFormat
name-value argument set to the"numeric"
value has no effect. A corresponding warning message appears in the Command Window.
Alternative Functionality
App
The fetch
function imports data using the command line. To
import data interactively, use the Database Explorer app.
Version History
Introduced in R2006bSee Also
Functions
close
|database
|databaseImportOptions
|setoptions
|getoptions
|reset
|execute
|databasePreparedStatement
|bindParamValues
|close
Topics
- Import Data from Database Table Using sqlread Function
- Retrieve Image Data Types
- Data Import Memory Management
- Customize Options for Importing Data from Database into MATLAB
- Importing Data Common Errors
- Import Data Using SQL Prepared Statement with Multiple Parameter Values
- SQL Prepared Statement Error Messages