fetchmulti

(Not recommended) Import data from multiple result sets

The fetchmulti function is not recommended. There is no replacement functionality.

There are no plans to remove the fetchmulti function at this time.

Description

example

curs = fetchmulti(curs) imports all rows of data from multiple result sets into the Data property of the cursor object. To create multiple result sets, first execute a SQL query using the exec function. The SQL query can contain two or more SELECT statements or call a stored procedure that consists of two or more SELECT statements. Then, use the fetchmulti function to import data in each result set.

Examples

collapse all

Import inventory and product data from a Microsoft® SQL Server® database into MATLAB® by using the cursor object and two SQL queries. Then, determine the highest quantity among inventory items.

Create an ODBC database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password. The database contains the tables inventoryTable and productTable.

datasource = 'MS SQL Server Auth';
conn = database(datasource,'','');

Check the database connection. If the Message property is empty, the connection is successful.

conn.Message
ans =

     []

Select all the data from inventoryTable and productTable by using two SELECT statements.

sqlquery = 'SELECT * FROM inventoryTable; SELECT * FROM productTable';
curs = exec(conn,sqlquery);

Import data from the two result sets. The fetchmulti function imports data into the Data property of the cursor object.

curs = fetchmulti(curs)
curs = 

  cursor with properties:

         Data: {[13×4 table]  [15×5 table]}
     RowLimit: 0
     SQLQuery: 'SELECT * FROM inventoryTable; SELECT * FROM productTable'
      Message: []
         Type: 'ODBCCursor Object'
    Statement: [1×1 database.internal.ODBCStatementHandle]

The Data property is a cell array consisting of cell arrays, tables, structures, or numeric matrices as specified in the setdbprefs function. The data type is the same for all result sets. Here, Data is a cell array of two tables. The tables contain the data from both result sets. The first table contains data from the first SELECT statement. The second table contains data from the second SELECT statement.

Display data from both tables.

inventory = curs.Data{1,1}
products = curs.Data{1,2}
inventory =

  13×4 table

    productNumber    Quantity    Price    inventoryDate
    _____________    ________    _____    _____________

     1               1700        15       '2014-09-23' 
     2               1200         9       '2014-07-08' 
     3                356        17       '2014-05-14' 
     4               2580        21       '2013-06-08' 
     5               9000         3       '2012-09-14' 
     6               4540         8       '2013-12-25' 
     7               6034        16       '2014-08-06' 
     8               8350         5       '2011-06-18' 
     9               2339        13       '2011-02-09' 
    10                723        24       '2012-03-14' 
    11                567        11       '2012-09-11' 
    12               1278        22       '2010-10-29' 
    13               1700        17       '2009-05-24' 


products =

  15×5 table

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

     9               1.2597e+05     1003              13          'Victorian Doll'  
     8               2.1257e+05     1001               5          'Train Set'       
     7               3.8912e+05     1007              16          'Engine Kit'      
     2               4.0031e+05     1002               9          'Painting Set'    
     4               4.0034e+05     1008              21          'Space Cruiser'   
     1               4.0035e+05     1001              14          'Building Blocks' 
     5               4.0046e+05     1005               3          'Tin Soldier'     
     6               4.0088e+05     1004               8          'Sail Boat'       
     3                 4.01e+05     1009              17          'Slinky'          
    10               8.8865e+05     1006              24          'Teddy Bear'      
    11               4.0814e+05     1004              11          'Convertible'     
    12               2.1046e+05     1010              22          'Hugsy'           
    13               4.7082e+05     1012              17          'Pancakes'        
    14                5.101e+05     1011              19          'Shawl'           
    15               8.9975e+05     1011              20          'Snacks'          

Determine the highest quantity among inventory items.

max(inventory.Quantity)
ans =

        9000

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

close(curs)

Close the database connection.

close(conn)

Input Arguments

collapse all

Database cursor, specified as a cursor object created using the exec function.

Output Arguments

collapse all

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

Introduced in R2006b