%% Customize Options to Omit Missing Data
% Customize import options when importing data from a database table. Control the import options by creating an SQLImportOptions object. Then, customize the import options to omit missing data. Import data using the sqlread function.
% This example uses the outages.csv file, which contains outage data. 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 outage information into the MATLAB® workspace.

outages = readtable("outages.csv");

% Create the outages database table using the outage information.

tablename = "outages";
sqlwrite(conn,tablename,outages)

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

opts = databaseImportOptions(conn,tablename);

%
% Determine the size of outages.

size(outages)

%
% Set the import options to omit rows that have missing data in the Customers variable.

varnames = "Customers";
opts = setoptions(opts,varnames,'MissingRule',"omitrow");

%
% Import the data and display the first eight rows. The imported data contains no missing data in the Customers variable.

T = sqlread(conn,tablename,opts);
head(T)

%
% Determine the size of T. The number of rows in the imported data is smaller because the software removes all rows with missing data in the Customers variable.

size(T)

%
% Delete the outages database table using the execute function.

sqlstr = "DROP TABLE ";
sqlquery = strcat(sqlstr,tablename);
execute(conn,sqlquery)

% Close the database connection.

close(conn)

% Copyright 2018 The MathWorks, Inc.