| Database Toolbox | ![]() |
Execute SQL statement and open cursor
Syntax
curs = exec(conn, 'sqlquery')
Description
curs = exec(conn, 'sqlquery')
executes the valid SQL statement sqlquery, against the database connection conn, and opens a cursor. Running exec returns the cursor object to the variable curs, and returns information about the cursor object. The sqlquery argument can also be a stored procedure for that database connection.
Use querytimeout to determine the maximum amount of time for which exec will try to complete the SQL statement.
You can have multiple cursors open at one time.
After opening a cursor, use fetch to import data from the cursor. Use resultset, rsmd, and statement to get properties of the cursor.
A cursor stays open until you close it using the close function. Always close a cursor after you finish using it.
Example 1 - Select All Data from Database Table
Select all data from the customers table accessed via conn. Assign the variable curs to the returned cursor object.
curs = exec(conn, 'select * from customers')
curs =
Attributes: []
Data: 0
DatabaseObject: [1x1 database]
RowLimit: 0
SQLQuery: 'select * from customers'
Message: []
Type: 'Database Cursor Object'
ResultSet: [1x1 sun.jdbc.odbc.JdbcOdbcResultSet]
Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
Statement: [1x1 sun.jdbc.odbc.JdbcOdbcStatement]
Fetch: 0
Example 2 - Select One Column of Data from Database Table
Select country data from the customers table accessed via conn. Assign the variable sqlquery to the SQL statement and assign curs to the returned cursor.
sqlquery = 'select country from customers'; curs = exec(conn, sqlquery);
Example 3 - Roll Back or Commit Data Exported to Database Table
Use exec to roll back or commit data after running an insert or an update for which the AutoCommit flag is off. To roll back data for conn, type
exec(conn, 'rollback')
exec(conn, 'commit');
Example 4 - Run Stored Procedure
Execute the stored procedure sp_customer_list for the database connection conn:
curs = exec(conn,'sp_customer_list');
See Also
close, database, fetch, insert, procedures, querytimeout, resultset, rsmd, set, update
| drivermanager | exportedkeys | ![]() |