%% Execute SQL Script and Return Results as Structures
% Connect to a MySQL® database. Then, run two SQL SELECT statements from the SQL script file compare_sales.sql. Import the results from the SQL queries as structures and perform simple sales data analysis. The file contains two SQL queries: the first SQL query retrieves sales of products from US suppliers, and the second retrieves sales of products from foreign suppliers.
%
% Create a MySQL native interface database connection to a MySQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.

datasource = "MySQLNative";
username = "root";
password = "matlab";
conn = mysql(datasource,username,password);

%
% Execute the SQL script. The SQL script contains two SQL queries that retrieve sales data from US and foreign suppliers, respectively. Specify structure as the data return format for the query results.

scriptfile = "compare_sales.sql";
results = executeSQLScript(conn,scriptfile, ...
    'DataReturnFormat',"structure")

% The executeSQLScript function returns a structure array that contains two structures in the Data field. The first structure contains the results of executing the first SQL query in the SQL script file. The second structure contains the results of executing the second SQL query.
%
% Display the imported data for the second SQL query in the SQL script file. The data contains sales results from foreign suppliers.

data = results(2).Data

%
% Determine the highest sales amount in January.

max(data.Jan_Sales)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.