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 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.
Examples
Input Arguments
Output Arguments
Limitations
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 tocellarray
,structure
, ornumeric
.The
fetch
function returns a warning when you set theVariableNamingRule
property of theSQLImportOptions
object to"preserve"
and set the'DataReturnFormat'
name-value pair argument tostructure
.The
fetch
function returns an error when you use the'VariableNamingRule'
name-value pair argument with theSQLImportOptions
objectopts
.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
, andVariableNames
.The length of each variable name must be less than the number returned by
namelengthmax
.
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 the'DataReturnFormat'
name-value pair 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
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