User`s guide
6 Using Database Toolbox Functions
6-40
Selecting Data Using the exec Function
In this section...
“About the exec Function” on page 6-40
“Using Cursor Objects” on page 6-40
“Working with Microsoft Excel” on page 6-41
“Database Considerations” on page 6-41
About the exec Function
Use the exec function to execute an SQL statement and return the database cursor
object. Here are some general points about using exec:
• Use Database Explorer to query databases as an alternative to using exec.
• exec supports the native ODBC interface.
• Unless noted in this reference page, the exec function supports all valid SQL
statements, such as nested queries.
• The sqlquery argument can be a stored procedure for the database connection of the
form {call sp_name (parm1,parm2,...)}.
• Use exec when the stored procedure returns one or more result sets. For procedures
that return output parameters, use runstoredprocedure.
Using Cursor Objects
• Check curs.Message to find any error messages returned from the database after
query execution. If you would like the error messages to be thrown to the MATLAB
command prompt, use setdbprefs as follows.
setdbprefs('Errorhandling','report');
curs = exec(conn,'select * invalidtablename')
To store error messages in curs.Message instead of sending them to the MATLAB
command prompt, use setdbprefs as follows.
setdbprefs('Errorhandling','store');
• After opening a cursor, use fetch to import data from the cursor. Use resultset,
rsmd, and access the Statement property to get properties of the cursor.
• You can have multiple cursors open at one time.