close
(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 Version History.
Syntax
Description
Examples
Close cursor
Object
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.
conn.Message
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); curs.Data(1:3,:)
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; max(data.unitCost)
ans = 24
After you finish working with the cursor
object, close it.
close(curs)
After you close the cursor
object, MATLAB deletes the object. Use the clear
function to remove
the curs
variable from the MATLAB workspace.
curs
clear curs
curs = handle to deleted cursor
Close the database connection.
close(conn)
Input Arguments
Version History
Introduced before R2006aR2018b: close
function is not recommended
The close
function is not recommended. Use the fetch
function to import data. Some differences between the workflows might
require updates to your code.
There are no plans to remove the close
function at this
time.
Use the fetch
function with the connection
object to import data from a database in one step.
In prior releases, you wrote multiple lines of code to create the cursor
object and import data. For example:
curs = exec(conn,sqlquery); curs = fetch(curs); results = curs.Data; close(curs)
Now you can import data in one step using the fetch
function.
results = fetch(conn,sqlquery);
There is no replacement functionality for the close
function.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)