Database Toolbox Previous page   Next Page
exec

Execute SQL statement and open cursor

Syntax

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 be a stored procedure for that database connection, of the form
{call sp_name (parm1,parm2,...)}

Remarks

Examples

Example 1--Select All Data from Database Table

Select all data from the customers table accessed via the database connection, conn. Assign the returned cursor object to the variable curs.

Example 2--Select One Column of Data from Database Table

Select country data from the customers table accessed via the database connection, conn. Assign the SQL statement to the variable sqlquery and assign the returned cursor to the variable curs.

Example 3--Use Variable in a Query

Select data from the customers table accessed via the database connection conn, where country is a variable. In this example, the user is prompted to supply their country, which is assigned to the variable UserCountry.

MATLAB prompts

The user responds

Without using a variable, the function to retrieve the data would be

To instead perform the query using the user's response, use

The select statement is created by using square brackets to concatenate the two strings 'select * from customers where country =' and 'UserCountry'.

Example 4--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 the database connection conn, type

To commit the data, type:

Example 5--Run Stored Procedure

Execute the stored procedure sp_customer_list for the database connection conn.

You can run a stored procedure with input parameters, for example

Example 6--Change Catalog

To change the catalog for the database connection conn to intlprice.

See Also

close, database, fetch, insert, procedures, querybuilder, querytimeout, resultset, rsmd, set, update

Data Retrieval Restrictions


Previous page  drivermanager exportedkeys Next page

© 1994-2005 The MathWorks, Inc.