Skip to Main Content Skip to Search
Product Documentation

database.fetch - Execute SQL statement to import data into MATLAB workspace

Syntax

results = fetch(conn, sqlquery)
results = fetch(conn, sqlquery, RowInc)

Description

results = fetch(conn, sqlquery) executes the SQL statement sqlquery, imports data for the open connection object conn, and returns the data to results. (For more information on SQL statements, see exec.)

results = fetch(conn, sqlquery, RowInc) imports RowInc rows of data at a time.

Input Arguments

conn

A database connection object.

sqlquery

A SQL statement.

RowInc

Specifies the number of rows of data to import at a time. Use RowInc when importing large amounts of data. Retrieving data in increments, as specified by RowInc, helps reduce overall retrieval time.

Output Arguments

results

A cell array, structure, or numeric matrix, depending on specifications set by setdbprefs.

Tips

Examples

This example shows how to import data. (If you experience speed and memory issues, use the rowinc argument

  1. Import the country column from the customers table in the SampleDB database.

    conn = database('SampleDB','','');
    setdbprefs('DataReturnFormat','cellarray')
    results = fetch(conn, 'select country from customers')
    results = 
    
        'Germany'
        'Mexico'
        'Mexico'
        'UK'
        'Sweden'
    
        ...
    
        'Finland'
        'Brazil'
        'USA'
        'Finland'
        'Poland'
  2. View the size of the cell array into which the results were returned.

    size(results)ans =
    
        91     1
 

Import two columns of data and view information about the data.

  1. Import the ProductName and Discontinued columns from the SampleDB database.

    conn = database('SampleDB', '', '');
    setdbprefs('DataReturnFormat','cellarray')
    results = fetch(conn, ['select ProductName, '...
       'Discontinued from Products']);
  2. View the size of the cell array into which the results were returned.

    size(results)
    ans =
    
        77   2
  3. View the results for the first row of data.

    results(1,:)
    ans = 
        'Chai'    [0]
  4. View the data type of the second element in the first row of data.

    class(results{1,2})
    ans =
    logical

See Also

cursor.fetch | database | exec | fetch | logical

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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