%% Initialize Parallel Pool with ODBC Database Connection
% Using an ODBC database connection, access a database using a parallel pool. Import data from multiple SQL queries in parallel.
% Each worker in the pool has the ODBC driver installed. Also, each worker has a configured ODBC data source. For more information, see the configureODBCDataSource function.
%
% Start the parallel pool.

pool = gcp;

%
% Initialize the parallel pool using an ODBC data source. This data source configures an ODBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.

datasource = "MS SQL Server Auth";
username = "";
password = "";
c = createConnectionForPool(pool,datasource,username,password);

%
% Define the SQL queries.

sqlqueries = ["SELECT * FROM invoice" ...
    "SELECT * FROM inventorytable" ...
    "SELECT * FROM producttable"];

%
% Use the parfor function to parallelize data access using the query basket.
% For each worker, retrieve the database connection object, execute the SQL queries, and import data locally.

parfor i = 1:length(sqlqueries)

    conn = c.Value;

    results = fetch(conn,sqlqueries(i));

    allresults{i} = results;

end

%
% Display the results for all queries. The cell array contains three tables, one for each set of query results.

allresults

%
% Close the parallel pool.

delete(pool);

% Copyright 2018 The MathWorks, Inc.