%% Retrieve Metadata Information About Imported Data
% Retrieve metadata information when importing data from a database table. Import data using the sqlread function and explore the metadata information by using dot notation.
% 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)

%
% Import the data into the MATLAB workspace and return metadata information about the imported data.

[data,metadata] = sqlread(conn,tablename);

%
% View the names of the variables in the imported data.

metadata.Properties.RowNames

%
% View the data type of each variable in the imported data.

metadata.VariableType

%
% View the missing data value for each variable in the imported data.

metadata.FillValue

%
% View the indices of the missing data for each variable in the imported data.

metadata.MissingRows

%
% Display the first eight rows of the imported data that contain missing restoration time. data contains restoration time in the fifth variable. Use the numeric indices to find the rows with missing data.

index = metadata.MissingRows{5,1};
nullrestoration = data(index,:);
head(nullrestoration)

%
% 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.