This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Import data from multiple resultsets

To retrieve multiple resultsets, use exec with a sqlquery statement. sqlquery can contain two or more SELECT statements or run a stored procedure consisting of two or more SELECT statements. Then, use fetchmulti to retrieve the data in each resultset.




curs = fetchmulti(curs) imports data from an open SQL cursor object curs that contains multiple resultsets into the object curs.


collapse all

Create a database connection conn to the Microsoft® SQL Server® database. To connect without Windows® authentication using the native ODBC interface, connect to the database with the ODBC data source name. Here, this code assumes that you are connecting to a data source named MS SQL Server with user name username and password pwd.

conn = database.ODBCConnection('MS SQL Server','username','pwd');

Select all data from two tables using two SELECT statements in sqlquery.

sqlquery = 'SELECT * FROM inventoryTable; SELECT * FROM productTable';

curs = exec(conn,sqlquery);

Import data from the two resultsets.

 curs = fetchmulti(curs)
curs = 

  cursor with properties:

         Data: {{23x4 cell}  {15x5 cell}}
     RowLimit: 0
     SQLQuery: 'SELECT * FROM inventoryTable; SELECT * FROM productT…'
      Message: []
         Type: 'ODBCCursor Object'
    Statement: [1x1 database.internal.ODBCStatementHandle]

curs.Data is a cell array consisting of cell arrays, tables, structures, or numeric matrices as specified in setdbprefs. The data type is the same for all resultsets.

curs.Data contains the data from both resultsets. The first cell array contains data from the first SELECT statement. The second cell array contains data from the second SELECT statement.

Display the data from both tables.

resultset_one = curs.Data{1,1}
resultset_two = curs.Data{1,2}
resultset_one = 

    [ 1.00]    [ 1700.00]    [ 14.50]    '2014-10-20 00:00:…' 
    [ 2.00]    [ 1200.00]    [  9.30]    '2014-10-20 00:00:…' 
    [ 3.00]    [  356.00]    [ 17.20]    '2014-10-20 00:00:…' 

resultset_two = 

  Columns 1 through 4

    [ 9.00]    [125970.00]    [1003.00]    [13.00]
    [ 8.00]    [212569.00]    [1001.00]    [ 5.00]
    [ 7.00]    [389123.00]    [1007.00]    [16.00]

  Column 5

    'Victorian Doll' 
    'Train Set'      
    'Engine Kit'     

After you finish working with the cursor object, close it. Close the database connection.


Related Examples

Input Arguments

collapse all

Database cursor, specified as an open SQL database cursor object created using exec.

Output Arguments

collapse all

Database cursor, returned as a database cursor object populated with fetched data in the Data property. You can specify the output data format in the Data property using setdbprefs.

See Also

| | |

Introduced in R2006b

Was this topic helpful?