%% Preserve Variable Names When Importing Data
% Import product data from a Microsoft® SQL Server® database table into MATLAB® by using an ODBC connection. The table 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 table productTable.

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)

%
% Import data from the database table productTable. The sqlread function returns a MATLAB table that contains the product data. Display the first three rows of the data in the table.

tablename = "productTable";
data = sqlread(conn,tablename);
head(data,3)

% The sqlread 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 = sqlread(conn,tablename, ...
    'VariableNamingRule',"preserve");
head(data,3)

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

close(conn)

%
% Copyright 2019 The MathWorks, Inc.