exec - Execute SQL statement and open cursor

GUI Alternatives

Query databases using Visual Query Builder. For more information on Visual Query Builder, see Using Visual Query Builder.

Syntax

curs = exec(conn, 'sqlquery')

Description

curs = exec(conn, 'sqlquery') executes the SQL statement sqlquery for the database connection conn, and opens a cursor.

Running exec returns the cursor object to the variable curs and returns additional 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 Data from a Database Table

Select data from the customers table that you access using the database connection conn. Assign the returned cursor object to the variable curs.

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 that you access using the database connection conn. Assign the SQL statement to the variable sqlquery and assign the returned cursor to the variable curs.

sqlquery = 'select country from customers';
curs = exec(conn, sqlquery);

Example 3 — Use a Variable in a Query

  1. Select data from the customers table that you access using the database connection conn, where country is a variable. In this example, you are prompted to specify your country. Your input is assigned to the variable UserCountry.

    UserCountry = input('Enter your country: ', 's')
    
  2. You are prompted as follows:

    Enter your country:
    

    Enter:

    Mexico
    
  3. To perform the query using your input, run:

    curs = exec(conn, ...
    		['select * from customers where country= ' '''' UserCountry ''''])
    curs=fetch(curs)
    

    The select statement is created by using square brackets to concatenate the two strings select * from customers where country = and 'UserCountry'. The pairs of four quotation marks are needed to create the pair of single quotation marks that appears in the SQL statement around UserCountry. The outer two marks delineate the next string to concatenate, and two marks are required inside them to denote a quotation mark inside a string.

Example 4 — Roll Back or Commit Data Exported to Database Table

Use exec to roll back or commit data after running a fastinsert, insert, or update for which the AutoCommit flag is off.

Example 5 — Change Database Connection Catalog

Change the catalog for the database connection conn to intlprice.

curs = exec(conn,'Use intlprice');

Example 6 — Create a Table and Add a New Column

This example creates a table and adds a new column to it.

  1. Use the SQL CREATE command to create the table.

    mktab = 'CREATE TABLE Person(LastName varchar, ...
    FirstName varchar,Address varchar,Age int)'
  2. Create the table for the database connection object conn.

    exec(conn, mktab);
    
  3. Use the SQL ALTER command to add a new column, City, to the table.

    a = exec(conn, ...
    'ALTER TABLE Person ADD City varchar(30)') 

Example 7 — Run a Simple Stored Procedure

Example 8 — Return a Cursor Object Using a Stored Procedure

The following example calls a database stored procedure that returns a cursor object.

  1. Specify data to return as a structure.

    setdbprefs('DataReturnFormat','structure');
    
  2. Define a stored procedure.

    ssql_cmd1 = '{?= call get_int_by_id(1,1, ...
    				to_date('07/02/05','MM/DD/YY'),...
    				to_date('07/07/05','MM/DD/YY'))}';
    
  3. Execute the stored procedure and open a cursor object.

    curs = exec(conn, ssql_cmd1)
    curs =
       Attributes: []
                 Data: 0
       DatabaseObject: [1x1 database]
             RowLimit: 0
             SQLQuery: [1x97 char]
              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
    
    
  4. Import data from the cursor to a MATLAB variable, a.

    a = fetch(curs);
    
  5. View a.Data.

    a.Data
    ans =
           TS_DT: {'2005-07-02 00:00:00.0'}
       INT_VALUE: 1  
    
  6. Define another stored procedure.

    sql_cmd2='{?= call nrg.ts_get_int_by_id(1,1,...
    				to_date(''07/02/05'',''MM/DD/YY''),...
    				to_date(''07/20/05'',''MM/DD/YY''))}';
    
  7. Repeat steps 1 through 5 using this new stored procedure.

    curs = exec(conn, ssql_cmd2)
    curs =
           Attributes: []
                 Data: 0
       DatabaseObject: [1x1 database]
             RowLimit: 0
             SQLQuery: [1x97 char]
              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
    
    a = fetch(curs)
    a =
           Attributes: []
                 Data: [1x1 struct]
       DatabaseObject: [1x1 database]
             RowLimit: 0
             SQLQuery: [1x97 char]
              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: ...
    							[1x1 com.mathworks.toolbox.database.fetchTheData]
    a.Data
    ans =
           TS_DT: {2x1 cell}
       INT_VALUE: [2x1 double]
    
  8. Examine the attributes of a.

    a.Data.TS_DT
    ans =
       '2005-07-02 00:00:00.0'
       '2005-07-10 00:00:00.0'
    a.Data.INT_VALUE
    ans =
        1
        6

See Also

close, cursor.fetch, database, database.fetch, fastinsert, fetch, procedures, querybuilder, querytimeout, resultset, rsmd, set, update, Using Visual Query Builder, Data Retrieval Restrictions

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS