Main Content


(Not recommended) Close cursor

The close function is not recommended. There is no replacement for this functionality. To import data, use the fetch function. For details, see Compatibility Considerations.



close(curs) closes and invalidates the database cursor and driver resource utilizer to free up database and driver resources.


collapse all

Connect to a Microsoft® SQL Server® database and verify the database connection. Then, import data from the database into MATLAB®. Determine the highest unit cost among the retrieved products in the table. Close the database cursor and database connection.

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 table productTable.

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

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

ans =


Select all data from the table productTable by using the connection object, and sort the data by product number. Assign the SQL SELECT statement to the variable sqlquery. The cursor object contains the executed SQL query.

sqlquery = 'SELECT * FROM productTable ORDER BY productNumber';
curs = exec(conn,sqlquery);

Import the data from the executed SQL query and display the first three rows.

curs = fetch(curs);
ans =

  3×5 table

    productNumber    stockNumber    supplierNumber    unitCost    productDescription
    _____________    ___________    ______________    ________    __________________

    1                4.0035e+05     1001              14          'Building Blocks' 
    2                4.0031e+05     1002               9          'Painting Set'    
    3                  4.01e+05     1009              17          'Slinky'          

Determine the highest unit cost in the table.

data = curs.Data;
ans =


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


After you close the cursor object, MATLAB deletes the object. Use the clear function to remove the curs variable from the MATLAB workspace.

clear curs
curs = 

  handle to deleted cursor

Close the database connection.


Input Arguments

collapse all

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

Version History

Introduced before R2006a

expand all

See Also

| |

External Websites