%% Retrieve Default Import Options for Database Columns Using Variable Names
% Control the import options by creating an SQLImportOptions object. Then, retrieve the default import options for several columns from a database table. Specify the columns to retrieve by using the database column names.
% This example uses the patients.xls spreadsheet, which contains the columns LastName, Age, and Location. Also, the example uses a Microsoft® SQL Server® Version 11.00.2100 database and the Microsoft SQL Server Driver 11.00.5058.
%
% Create a database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.

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

%
% Load patient information into the MATLAB® workspace.

patients = readtable('patients.xls');

% Create the patients database table using the patient information.

tablename = 'patients';
sqlwrite(conn,tablename,patients)

%
% Create an SQLImportOptions object using the patients database table and the databaseImportOptions function.

opts = databaseImportOptions(conn,tablename);

%
% Specify the names of the database columns in the patients database table.

varnames = {'LastName','Age','Location'};

% Retrieve and display the default import options for the specified database columns.

varOpts = getoptions(opts,varnames)

% To modify the variable import options, see the setoptions function.
%
% Delete the patients database table using the execute function.

sqlquery = ['DROP TABLE ' tablename];
execute(conn,sqlquery)

% Close the database connection.

close(conn)

% Copyright 2018 The MathWorks, Inc.