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
Name-Value 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
fetchfunction returns an error if you specify theVariableNamingRulename-value argument and set theDataReturnFormatname-value argument to"cellarray","structure", or"numeric".The
fetchfunction returns a warning if you set theVariableNamingRuleproperty of theSQLImportOptionsobject to"preserve"and set theDataReturnFormatname-value argument to"structure".The
fetchfunction returns an error if you use theVariableNamingRulename-value argument with theSQLImportOptionsobjectopts.If you set the
VariableNamingRulename-value argument to the value"modify":These variable names are reserved identifiers for the
tabledata 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
fetchfunction returns an error if you specify theRowFiltername-value argument with theSQLImportOptionsobjectopts. It is ambiguous which of theRowFilterobject 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 BYcommand in yoursqlquerystatement.For Microsoft® Excel®, tables in
sqlqueryare 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
SELECTstatements 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
fetchfunction with theoptsinput argument and theDataReturnFormatname-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