User`s guide

6 Using Database Toolbox Functions
6-14
Create a Query Using Special Characters
This example shows how to write an SQL query for table names or columns names with
special characters.
These characters require using escape characters that are specific to your database.
Consult your database documentation for the right escape characters. This example uses
a Microsoft SQL Server database.
Connect to the Database
Connect to the Microsoft SQL Server database using a JDBC driver without operating
system authentication. For example, this code assumes you are connecting to a database
named dbname with the user name username, password pwd, database server name
sname, and port number 123456.
conn = database('dbname','username','pwd',...
'Vendor','Microsoft SQL Server','Server','sname',...
'AuthType','Server','portnumber',123456);
Create a Query with Special Characters
Suppose you want to select all data in a column with a column name that contains
spaces. This column resides in a table with a table name that contains spaces. A space is
a special character that needs to be enclosed by escape characters for the SQL query to
execute. Brackets are the escape characters for a Microsoft SQL Server database. Create
an SQL query string sqlquery that contains the column name and table name enclosed
by brackets.
sqlquery = 'select [column with spaces] from [table with spaces]';
Execute the Query
Execute the SQL query using the exec function and display the data using the fetch
function.
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
ans =
'some text'