%% Preserve Variable Names in Joined Data
% Import joined product data from two Microsoft® SQL Server® database tables into MATLAB® by using an ODBC connection. One of the tables contains a variable name with a non-ASCII character. When importing data, preserve the names of all the variables.
%
% Create an ODBC database connection to an SQL Server database with Windows® authentication. Specify a blank user name and password. The database contains the tables productTable and suppliers.

datasource = "MSSQLServerAuth";
conn = database(datasource,"","");

%
% Check the database connection. If the Message property is empty, then the connection is successful.

conn.Message

%
% Add a column to the database table productTable. The column name contains a non-ASCII character.

sqlquery = "ALTER TABLE productTable ADD tamaño varchar(30)"; 
execute(conn,sqlquery)

%
% Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Specify the number of rows to return using the 'MaxRows' name-value pair argument.

lefttable = 'productTable'; 
righttable = 'suppliers'; 
data = sqlouterjoin(conn,lefttable,righttable,'MaxRows',3) 

% data is a table that contains three of the matched rows from the two tables. The sqlouterjoin function converts the name of the new variable into ASCII characters.
%
% Preserve the name of the variable that contains the non-ASCII character by specifying the VariableNamingRule name-value pair argument. Import the data again.

data = sqlouterjoin(conn,lefttable,righttable,'MaxRows',3, ...
    'VariableNamingRule',"preserve") 

% The sqlouterjoin function preserves the non-ASCII character in the variable name.
%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.