For general information about this feature, see SQL Pass-Through Facility.
Here are the SQL pass-through facility specifics for the ODBC interface.
ODBC.odbc alias is used. The functionality
of multiple connections to the same ODBC data source might be limited
by the particular data source driver.These examples use ODBC
to connect to a data source that is configured under the data source
name User's Data using the alias USER1.
The first example uses the connection method that is guaranteed to
be present at the lowest level of ODBC conformance. DATASRC= names
can contain quotation marks and spaces.
proc sql;
connect to ODBC as user1
(datasrc="User's Data" user=myusr1 password=mypwd1);
This example uses the connection method that represents a more advanced level of ODBC conformance. It uses the input dialog box that is provided by the driver. The DATASRC= and USER= arguments are within the connection string. The SQL pass-through facility therefore does not parse them but instead passes them to the ODBC manager.
proc sql;
connect to odbc as user1
(required="dsn=User's Data;uid=myusr1");
This example enables you to select any data source that is configured on your machine. The example uses the connection method that represents a more advanced level of ODBC conformance, Level 1. When connection succeeds, the connection string is returned in the SQLXMSG and SYSDBMSG macro variables. The connection string can be stored if this method is used to configure a connection for later use.
proc sql;
connect to odbc (required);
This next example prompts you to specify the information that is required to make a connection to the DBMS. You are prompted to provide the data source name, user ID, and password in the dialog boxes that are displayed.
proc sql;
connect to odbc (prompt);
This example sends an Oracle SQL query (presented in highlighted text) to the Oracle database for processing. The results from the query serve as a virtual table for the PROC SQL FROM clause. In this example, MYCON is a connection alias.
proc sql;
connect to odbc as mycon
(datasrc=mysrv1 user=myusr1 password=mypwd1);
select *
from connection to mycon
(select empid, lastname, firstname,
hiredate, salary
from sasdemo.employees
where hiredate>='31.12.1988');
disconnect from mycon;
quit;
This next example gives the previous query a name and stores it as the SQL view Samples.Hires88. The CREATE VIEW statement appears highlighted.
libname samples 'SAS-library';
proc sql;
connect to odbc as mycon
(datasrc=mysrv1 user=myusr1 password=mypwd1);
create view samples.hires88 as
select *
from connection to mycon
(select empid, lastname, firstname,
hiredate, salary from sasdemo.employees
where hiredate>='31.12.1988');
disconnect from mycon;
quit;
This example connects to Microsoft Access and creates a view NEWORDERS from all columns in the ORDERS table.
proc sql;
connect to odbc as mydb
(datasrc=MSAccess7);
create view neworders as
select * from connection to mydb
(select * from orders);
disconnect from mydb;
quit;
This next example sends
an SQL query to Microsoft SQL Server, configured under the data source
name SQL Server, for processing. The
results from the query serve as a virtual table for the PROC SQL FROM
clause.
proc sql;
connect to odbc as mydb
(datasrc="SQL Server" user=myusr1 password=mypwd1);
select * from connection to mydb
(select CUSTOMER, NAME, COUNTRY
from CUSTOMERS
where COUNTRY <> 'USA');
quit;
This example returns a list of the columns in the CUSTOMERS table.
proc sql;
connect to odbc as mydb
(datasrc="SQL Server" user=myusr1 password=mypwd1);
select * from connection to mydb
(ODBC::SQLColumns (, , "CUSTOMERS"));
quit;
SAS/ACCESS Interface to ODBC supports the following special queries. Many databases provide or use system tables that allow queries to return the list of available tables, columns, procedures, and other useful information. ODBC provides much of this functionality through special application programming interfaces (APIs) to accommodate databases that do not follow the SQL table structure. You can use these special queries on SQL and non-SQL databases.
Here is the general format of the special queries:
required to distinguish special queries from regular queries.
is the specific API that is being called. Neither ODBC:: nor SQLAPI are case sensitive.
a quoted string that is delimited by commas.
Within the quoted string, two characters are universally recognized: the percent sign (%) and the underscore (_). The percent sign matches any sequence of zero or more characters; the underscore represents any single character. Each driver also has an escape character that can be used to place characters within the string. See the driver documentation to determine the valid escape character.
The values for the special query arguments are
DBMS-specific. For example, you provide the fully qualified table name for a
"Catalog" argument. In dBase, the value of
"Catalog" might be
c:\dbase\tst.dbf and in Microsoft SQL Server, the value might
be test.customer. In addition, depending on the DBMS that you
are using, valid values for a "Schema" argument
might be a user ID, a database name, or a library. All arguments are optional. If
you
specify some but not all arguments within a parameter, use a comma to indicate the
omitted arguments. If you do not specify any parameters, commas are not necessary.
Special queries are not available for all ODBC drivers.
ODBC supports these special queries:
returns a list of all columns that match the specified arguments. If no arguments are specified, all accessible column names and information are returned.
returns a list of all column privileges that match the specified arguments. If no arguments are specified, all accessible column names and privilege information are returned.
returns a list of database aliases to which ODBC is connected.
returns a list of DB2 databases (DSNs) to which ODBC is connected. It returns one row with two columns that describe the DBMS name (such as Microsoft SQL Server or Oracle) and the corresponding DBMS version.
returns a list of all columns that comprise foreign keys that match the specified arguments. If no arguments are specified, all accessible foreign key columns and information are returned.
returns information about the data types that are supported in the data source.
returns a list of all columns that compose the primary key that matches the specified table. A primary key can be composed of one or more columns. If no table name is specified, this special query fails.
returns a list of all procedures that match the specified arguments. If no arguments are specified, all accessible procedures are returned.
returns a list of all procedure columns that match the specified arguments. If no arguments are specified, all accessible procedure columns are returned.
returns a list of the optimal set of columns that uniquely identify a row in the specified table.
returns a list of the statistics for the specified table name. You can specify the SQL_INDEX_ALL and SQL_ENSURE options in the SQLStatistics API call. If the table name argument is not specified, this special query fails.
returns a list of all tables that match the specified arguments. If no arguments are specified, all accessible table names and information are returned.
returns a list of all tables and associated privileges that match the specified arguments. If no arguments are specified, all accessible table names and associated privileges are returned.