Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10: Accessing Relational Databases (Self-Study)

Similar presentations


Presentation on theme: "Chapter 10: Accessing Relational Databases (Self-Study)"— Presentation transcript:

1 Chapter 10: Accessing Relational Databases (Self-Study)
2 Chapter 10: Accessing Relational Databases (Self-Study) 10.1 LIBNAME Access to DBMS Data 10.2 Executing DBMS-specific SQL Using the SQL Pass-Through Facility

2 Chapter 10: Accessing Relational Databases (Self-Study)
2 Chapter 10: Accessing Relational Databases (Self-Study) 10.1 LIBNAME Access to DBMS Data 10.2 Executing DBMS-specific SQL Using the SQL Pass-Through Facility

3 Objectives Connect to a database management system (DBMS) using the LIBNAME statement. Use DBMS tables in PROC SQL statements. Disconnect from the DBMS.

4 The LIBNAME Statement (Review)
The LIBNAME statement can do the following: establish a libref that acts as a nickname pointing to a collection of data sets (SAS data library) enable referencing of data sets by a two-level name make operating environment-specific references in the remaining program code unnecessary Accessing a SAS library via a libref does not override authentication/permissions restrictions set by the operating system.

5 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; ...

6 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop ...

7 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; proc sql; select * from orion.customer; quit; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop ...

8 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; proc sql; select * from orion.customer; quit; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop ...

9 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; proc sql; select * from orion.customer; quit; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop ...

10 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; proc sql; select * from orion.customer; quit; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop Portable code: No O/S-specific information ...

11 The LIBNAME Statement (Review)
General form of the LIBNAME statement: LIBNAME libref 'SAS-data-library location' <options>; Example (Windows operating system): O/S-specific information libname orion 's:\workshop'; proc sql; select * from orion.customer; quit; customer.sas7bdat employee_addresses.sas7bdat employee_data.sas7bdat employee_donations.sas7bdat s:\workshop Portable code: No O/S-specific information

12 LIBNAME Statements Using Access Engines
Syntax is modified to provide information required by the DBMS and SAS/ACCESS engine. The assigned libref acts as a nickname for the DBMS. DBMS tables are referenced using two-level names, in the same manner as native SAS data sets. Read access and Write access are controlled by the DBMS authentication/authorization system.  SAS/ACCESS engines can be licensed separately from Base SAS. Check with your SAS Administrator for details specific to your site.

13 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; ...

14 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; Example (Windows O/S - Microsoft Access): libname mydbms ACCESS "s:\workshop\sql1.mdb"; Microsoft Access Data Customer Customer ...

15 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; Example (Windows O/S - Microsoft Access): libname mydbms ACCESS "s:\workshop\sql1.mdb"; Microsoft Access Data proc sql; select * from mydbms.customer; quit; Customer Customer ...

16 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; Example (Windows O/S - Microsoft Access): libname mydbms ACCESS "s:\workshop\sql1.mdb"; Microsoft Access Data proc sql; select * from mydbms.customer; quit; Customer Customer ...

17 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; Example (Windows O/S - Microsoft Access): libname mydbms ACCESS "s:\workshop\sql1.mdb"; Microsoft Access Data proc sql; select * from mydbms.customer; quit; Customer Customer ...

18 The LIBNAME Statement (Review)
General form of the LIBNAME statement using a SAS/ACCESS engine: LIBNAME libref <engine> <engine-specific-options>; Example (Windows O/S - Microsoft Access): libname mydbms ACCESS "s:\workshop\sql1.mdb"; Microsoft Access Data proc sql; select * from mydbms.customer; quit; Customer Portable code: No O/S-specific information

19 SAS/ACCESS LIBNAME Statement Examples
Oracle: libname mylib oracle user=UserID pw=password path=dbmspath schema=dbms_schema; DB2 (z/OS): libname mylib db2 authid=UserID ssid=db2; DB2 (UNIX/PC): libname mylib db2 user=UserID pw=password schema=dbms_schema; ODBC: libname libname mylib odbc datasrc=sql1;  Syntax varies by DBMS engine and operating system. Contact your SAS Administrator for information specific to your site.

20 The LIBNAME Statement – Disconnecting
General form of the LIBNAME statement when de-allocating a SAS library or disconnecting from a DBMS: LIBNAME libref CLEAR; Example: libname mydbms clear;

21 Accessing DBMS Data with the LIBNAME Statement
This demonstration includes the following tasks: assigning a libref to a Microsoft Access database creating, querying, and dropping a table in the DBMS using standard PROC SQL syntax s110d01

22 Chapter 10: Accessing Relational Databases (Self-Study)
2 Chapter 10: Accessing Relational Databases (Self-Study) 10.1 LIBNAME Access to DBMS Data 10.2 Executing DBMS-specific SQL Using the SQL Pass-Through Facility

23 Objectives Connect to a DBMS using the CONNECT statement.
Issue native SQL statements to the DBMS for execution. Disconnect from the DBMS.

24 Overview The SQL Procedure Pass-Through Facility communicates with the DBMS through the SAS/ACCESS engine. The facility enables you to do the following: pass native DBMS SQL statements to a DBMS display query results formatted by PROC SQL save query results as SAS data files create SAS data views containing pass-through queries

25 Overview SQL statements you can submit to the DBMS include these statements: SELECT statements (pass-through queries), which return output to SAS in a manner similar to in-line views EXECUTE statements, which include SQL statements that do not produce output (for example, CREATE TABLE, UPDATE, and DELETE)

26 SQL Procedure Pass-Through Queries
An SQL pass-through query consists of the statements listed below: a CONNECT statement a SELECT expression a DISCONNECT statement

27 Connecting to the DBMS The CONNECT statement can do the following:
specify the DBMS name provide other DBMS-specific connection information (optional for some databases) establish the connection to the DBMS specify an alias for the connection (optional)

28 Connecting to the DBMS General form of the CONNECT statement:
CONNECT TO dbms-engine <AS alias> (options);

29 Connecting to Microsoft Access
Example: Connect to a Microsoft Access table. proc sql; connect to ACCESS as mydbms (path="./sql1.mdb"); s110d02

30 The SELECT Expression The PROC SQL SELECT expression for SQL pass-through consists of these elements: a PROC SQL SELECT expression a FROM CONNECTION TO component the SQL code to be passed to the DBMS other PROC SQL clauses necessary to finish processing the data returned by the pass-through query

31 The SELECT Expression The query passed to the DBMS
uses DBMS-specific SQL syntax that will be processed inside the DBMS and not by SAS must reference only DBMS table column names is enclosed in matching parentheses.  A pass-through query must have valid DBMS SQL syntax. PROC SQL does not evaluate the syntax of pass-through queries.

32 SELECT from Microsoft Access
Example: Select from pass-through to Microsoft Access. The SELECT statement containing the FROM CONNECTION TO clause uses the data returned by the pass-through query in a manner similar to an in-line view. proc sql; connect to ACCESS as mydbms (path="./sql1.mdb"); title "Report using SQL Passthrough"; select * from connection to mydbms (select Product_Name, Price from Catalog_2007 order by Product_Name) ; s110d02

33 SELECT from Microsoft Access
Example: Combine a pass-through and SAS data. proc sql; connect to ACCESS as mydbms (path="./sql1.mdb"); title "SQL Passthrough and SAS Tables"; select of.Product_ID, Product_Name, Price, count(*) as Number_Sold from connection to mydbms (select Product_ID, Product_Name, Price from Catalog_2007 order by Product_Name) as pt, orion.Order_Fact as of where of.Product_ID=pt.Product_ID group by of.Product_ID ; s110d02

34 Closing the DBMS Connection
You can close the connection to the DBMS by either of these methods: submitting a DISCONNECT statement terminating the SQL procedure, for example, with a QUIT statement General form of the DISCONNECT statement: DISCONNECT FROM dbms-engine | alias;

35 Closing the Connection to Microsoft Access
Example: Disconnect from Microsoft Excel or Access. proc sql; connect to ACCESS as mydbms (path="./sql1.mdb"); title "Report using SQL Passthrough"; select * from connection to mydbms (select Product_Name, Price from Catalog_2007 order by Product_Name) ; disconnect from mydbms; quit; s110d02

36 Accessing DBMS Data with the LIBNAME Statement
This demonstration includes the following: using a CONNECT statement to access DBMS data from a PROC SQL invocation using the SQL Pass-Through Facility to execute DBMS-specific SQL to do the tasks: modify a DBMS table’s structure create and drop a DBMS view query DBMS tables and views s110d02


Download ppt "Chapter 10: Accessing Relational Databases (Self-Study)"

Similar presentations


Ads by Google