2012 sql challenges between sas & oracle group presentations... · oracle examples all examples...

50
SQL Challenges Between SAS & Oracle Joel Choy Nov 27, 2012 1

Upload: phamquynh

Post on 29-Aug-2019

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SQL Challenges Between SAS & Oracle

Joel Choy

Nov 27, 2012

1

Page 2: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

• Why SQL • Run Options • Problems • Oracle Examples • SAS Examples • Summary

2

Page 3: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Why SQL

Proc sort data=hr;

by empno;

Run;

Data temp (empno salary);

set HR

where salary > 10000;

Run;

Proc print data=temp;

Run;

Proc SQL;

select empno, salary from hr

where salary > 10000

order by empno;

Quit;

3

Page 4: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Why SQL

Proc sort data=hr; out=dep_salary;

by depno;

Run;

Proc summary data=dep_salary;

By depno ;

var salary;

output out=dep_sum_salary sum=;

Run;

Proc sql;

Create table dep_sum_salary as

select depno, sum( salary)

from hr

group by depno

order by 1;

Quit;

4

Page 5: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Why SQL

SAS SQL follows ANSI standards

Select column <, column>...

From table <, table>...

<where expression>

<group by column <, column>

<having expression>

<order by column <, column>

5

Page 6: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Run Options

6

Page 7: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Run Options

7

Page 8: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Run Options

8

Page 9: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Problems

ERROR: The SAS/ACCESS Interface to ORACLE cannot be loaded. ERROR: Image

SASORA found but not loadable.

ERROR: A Connection to the ORACLE DBMS is not currently supported, or is not installed at your site.

Solution: Install the correct Oracle client e.g. Oracle 10g client for PC sas 9.1.3

Or Make sure your PC path contains the Oracle 10g client bin directory ahead of other Oracle library bin.

9

Page 10: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Problems The following DOS prompt screen shows path has been fixed to search oracle_client_10

[C:\ORACLE\ORAHOME_10G\bin) ahead of other Oracle bin (e.g. Oracle 10gR2 data base bin (c:\oracle\product\10.2.0\db_1\bin;c:\ORA6I\bin]

Microsoft Windows [Version 6.0.6002] Copyright (c) 2006 Microsoft Corporation. All rights reserved. H:\>c: C:\>path PATH=C:\Program Files\Oracle\jre\1.1.7\bin;C:\ORACLE\ORAHOME_10G\bin;C:\oracle\p roduct\10.2.0\db_1\bin;C:\ORA6I\bin;C:\Program Files\CA\SharedComponents\PEC\bin ;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Win dowsPowerShell\v1.0\;C:\Program Files\Common Files\Lenovo;C:\ORANT\dev21\bin;C:\ ORANT\DEV12\BIN;C:\Program Files\NetManage\Windows;C:\Program Files\QuickTime\QT System\;C:\Program Files\Windows Imaging\;C:\Program Files\Microsoft Application Virtualization Client;C:\Program Files\SAS\Shared Files\Formats;C:\des_817\bin; C:\Program Files\TortoiseSVN\bin C:\>

10

Page 11: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Problems

How to set path variable ?

If you have administrative rights to your work PC, then follow below step : ( otherwise ask IT staff to change the path for you)

In DOS prompt window, enter path to display the current path

Click the upper left small dos icon, select edit/select all/click left mouse button

Repeat step 2 select edit/Copy/click left mouse button

paste (CTL+V) to Notepad

Edit the path to put the path you want ahead of other path(s) and copy (CTL+C)

Go back to the DOS prompt window and enter set path= and paste (CTL+C) the entire path after =

OR

If you have Vista

Right click Computer / Properties / Advanced System Settings ; select Advanced tab then click Environment Variables

Under System Variables, locate and select Path and click edit

In the variable value you would copy and paste the new values

11

Page 12: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples Proc SQL <stimer noerrorstop>; connect to oracle <as alias> (user=oracle_schema,pw=oracle_schema_pw,path=

oracle_db_name<,buffsize=dbindex=,insertbuff=,readbuff=,updatebuff=>)

<create table sas_table_name as> select * from connection to oracle | alias ({oracle sql select statements here } ); <%put &sqlxmsg;> <Disconnect from oracle;> Quit;

12

Page 13: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

Ex01 – simpliest code

proc sql ;

CONNECT TO ORACLE (USER=hr PW=orcl PATH='‘);

SELECT * FROM connection to oracle (

select * from hr.countries where rownum < 10

);

Quit;

13

Page 14: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2

for Vista 32 bits Data Base installed on my work PC with default dbname=orcl, all demo schemas pw are the same = orcl

/* EX01 - simpliest oracle pass through syntax */

/* EX02 - what stimer, noerrorstop %put sqlxmsg do ? */

/* EX03 - would %put &sqlxmsg help us ? */

/*EX04 - &sqlxmsg seems redudant */

/* EX05 -can we simplify syntax with macros ? Yes */

/* EX06 - more examples with macros to pass through */

/* EX07 - multiple schema connections to pass through */

/* EX08 - execute examples for non-sql select stmts */

/* EX09 - oracle hints for the oracle optimizer */

14

Page 15: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

19 /* EX01 - simpliest oracle pass through syntax 19 ! */ 20 /* with just 3 parms: user=, pw=, path='' 20 ! */ 21 /* path='' means use sample oracle 11gR2 data base installed on my 21 ! */ 22 /* pc. Actually you can even skip path= in this case but you will 22 ! */ 23 /* need to path= to specify connect entry for your oracle data base 23 ! */ 24 /* e.g. in Healthideas, path=dssp means connect to data base dssp 24 ! */ 25 proc sql ; 26 CONNECT TO ORACLE (USER=hr PW=XXXX path='' ); 27 SELECT * FROM connection to oracle ( 28 select * from hr.countries where rownum < 10 29 ); 30 Quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.90 seconds cpu time 0.03 seconds

15

Page 16: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

Ex01 output

16

Page 17: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

/* EX02 - what does stimer, noerrorstop %put sqlxmsg disconnect buy us ? */

/* not very much except when we have problems */

proc sql stimer noerrorstop; CONNECT TO ORACLE (USER=hr PW=orcl PATH=''); SELECT * FROM connection to oracle ( select * from hr.countries where rownum < 10 ); %put &sqlxmsg; disconnect from oracle; Quit;

17

Page 18: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

31 /* EX02 - what does stimer, noerrorstop %put sqlxmsg disconnect buy

31 ! us ? */

32 /* not very much except when we have problems

32 ! */

33 proc sql stimer noerrorstop;

NOTE: SQL Statement used (Total process time):

real time 0.00 seconds

cpu time 0.01 seconds

34 CONNECT TO ORACLE (USER=hr PW=XXXX PATH='');

NOTE: SQL Statement used (Total process time):

real time 0.90 seconds

cpu time 0.06 seconds

35 SELECT * FROM connection to oracle (

36 select * from hr.countries where rownum < 10

37 );

NOTE: SQL Statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

38 %put &sqlxmsg;

39 disconnect from oracle;

NOTE: SQL Statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

40 Quit;

NOTE: PROCEDURE SQL used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

18

Page 19: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

• /* EX03 - would %put &sqlxmsg help us ? */ • /* try to force an oracle sql syntax error */ • /* by selecting a non-existant table country instead of countries

*/ • proc sql stimer noerrorstop; • CONNECT TO ORACLE (USER=hr PW=orcl PATH=''); • SELECT * FROM connection to oracle ( • select * from hr.country where rownum < 10 • ); • %put &sqlxmsg; • disconnect from oracle; • Quit;

19

Page 20: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples • 41 /* EX03 - would %put &sqlxmsg help us ? • 41 ! */ • 42 /* try to force an oracle sql syntax error • 42 ! */ • 43 /* by selecting a non-existant table country instead of countries • 43 ! */ • 44 proc sql stimer noerrorstop; • NOTE: SQL Statement used (Total process time): • real time 0.00 seconds • cpu time 0.00 seconds

• 45 CONNECT TO ORACLE (USER=hr PW=XXXX PATH=''); • NOTE: SQL Statement used (Total process time): • real time 1.02 seconds • cpu time 0.01 seconds

• 46 SELECT * FROM connection to oracle ( • 47 select * from hr.country where rownum < 10 • 48 ); • ERROR: ORACLE prepare error: ORA-00942: table or view does not exist. • SQL statement: select * from hr.country where rownum < 10. • NOTE: SQL Statement used (Total process time): • real time 0.05 seconds • cpu time 0.00 seconds

• 49 %put &sqlxmsg; • ORA-00942: table or view does not exist • 50 disconnect from oracle; • NOTE: SQL Statement used (Total process time): • real time 0.00 seconds • cpu time 0.00 seconds

• 51 Quit; • NOTE: PROCEDURE SQL used (Total process time): • real time 0.00 seconds • cpu time 0.00 seconds

20

Page 21: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

• /* EX04 - would %put &sqlxmsg help us ? */

• /* try to force an oracle sql syntax error */ • /* not really, can do without it */ • proc sql; • CONNECT TO ORACLE (USER=hr PW=orcl PATH=''); • SELECT * FROM connection to oracle ( • select * from hr.country where rownum < 10 • ); • * %put &sqlxmsg; • disconnect from oracle; • Quit;

21

Page 22: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

52 /* EX04 - would %put &sqlxmsg help us ? 52 ! */ 53 /* try to force an oracle sql syntax error 53 ! */ 54 /* not really, can do without it 54 ! */ 55 proc sql; 56 CONNECT TO ORACLE (USER=hr PW=XXXX PATH=''); 57 SELECT * FROM connection to oracle ( 58 select * from hr.country where rownum < 10 59 ); ERROR: ORACLE prepare error: ORA-00942: table or view does not exist. SQL statement: select * from hr.country where rownum < 10. 60 * %put &sqlxmsg; 61 disconnect from oracle; 62 Quit; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE SQL used (Total process time): real time 0.91 seconds cpu time 0.03 seconds

22

Page 23: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

/* EX05 - can we simplify the pass through syntax with macros ? */ /* Yes ! */ %macro oracle_select(stmt); proc sql ; CONNECT TO ORACLE (USER=hr PW=orcl PATH=''); SELECT * FROM connection to oracle ( &stmt ); disconnect from oracle; quit; %mend; %oracle_select(select * from hr.countries where rownum < 10);

23

Page 24: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

63 /* EX05 - can we simplify the pass through syntax with macros ? */ 64 /* Yes ! */ 65 66 %macro oracle_select(stmt); 67 proc sql ; 68 CONNECT TO ORACLE (USER=hr PW=orcl PATH=''); 69 70 71 SELECT * FROM connection to oracle ( 72 &stmt 73 ); 74 75 disconnect from oracle; 76 quit; 77 %mend; 78 79 %oracle_select(select * from hr.countries where rownum < 10); NOTE: PROCEDURE SQL used (Total process time): real time 0.89 seconds cpu time 0.01 seconds

24

Page 25: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

• /* EX06 macro oracle_connect to connect to a schema@orcl only */ • /* macro oracle_select to execute oracle sql stmt only */ • /* macro oracle_disconnect to disconnect from orcl +quit */ • /* note the importance of quit stmt and ability to invoke macros */ • /* between 1 proc sql and 1 quit only */ • /* dealing with 1 schema only */

• %macro oracle_connect(schema); • proc sql ; • CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); • %mend;

• %macro oracle_select(stmt);

• SELECT * FROM connection to oracle ( • &stmt • );

• %mend;

• %macro oracle_disconnect; • disconnect from oracle; • quit; • %mend; • • %oracle_connect(hr); • %oracle_select(select * from hr.countries); • %oracle_select(select * from hr.jobs); • %oracle_disconnect;

25

Page 26: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples 80 /* EX06 macro oracle_connect to connect to a schema@orcl only 80 ! */ 81 /* macro oracle_select to execute oracle sql stmt only 81 ! */ 82 /* macro oracle_disconnect to disconnect from orcl +quit 82 ! */ 83 /* note the importance of quit stmt and ability to invoke 83 ! macros */ 84 /* between 1 proc sql and 1 quit only 84 ! */ 85 /* dealing with 1 schema only 85 ! */ 86 87 %macro oracle_connect(schema); 88 proc sql ; 89 CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); 90 %mend; 91 92 %macro oracle_select(stmt); 93 94 SELECT * FROM connection to oracle ( 95 &stmt 96 ); 97 98 %mend; 99 100 %macro oracle_disconnect; 101 disconnect from oracle; 102 quit; 103 %mend; 104 105 %oracle_connect(hr); 106 %oracle_select(select * from hr.countries); 107 %oracle_select(select * from hr.jobs); 108 %oracle_disconnect; NOTE: PROCEDURE SQL used (Total process time): real time 0.93 seconds cpu time 0.06 seconds

26

Page 27: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

EX06 output

27

Page 28: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

EX06 Output

28

Page 29: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

/* EX07 same as example 06 except separate oracle connection and oracle select */ /* note multiple oracle connections to different schemas: hr@orcl sh@orcl */ /* note also mulitple proc sql, quit with no disconnect from oracle connection */ %macro oracle_connect(schema); proc sql ; CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); %mend; %macro oracle_select(stmt); SELECT * FROM connection to oracle ( &stmt ); quit; %mend; %oracle_connect(hr); %oracle_select(select * from hr.countries where rownum < 5); %oracle_connect(sh); %oracle_select(select * from sh.channels);

29

Page 30: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples 109 /* EX07 same as example 06 except separate oracle connection and 109! oracle select */ 110 /* note multiple oracle connections to different schemas: 110! hr@orcl sh@orcl */ 111 /* note also mulitple proc sql, quit with no disconnect from 111! oracle connection */ 112 113 %macro oracle_connect(schema); 114 proc sql ; 115 CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); 116 %mend; 117 118 %macro oracle_select(stmt); 119 120 SELECT * FROM connection to oracle ( 121 &stmt 122 ); 123 quit; 124 125 %mend; 126 127 %oracle_connect(hr); 128 %oracle_select(select * from hr.countries where rownum < 5); NOTE: PROCEDURE SQL used (Total process time): real time 0.91 seconds cpu time 0.06 seconds 129 130 %oracle_connect(sh); 131 %oracle_select(select * from sh.channels); NOTE: PROCEDURE SQL used (Total process time): real time 0.96 seconds cpu time 0.03 seconds

30

Page 31: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

EX07 output

31

Page 32: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

EX07 output

32

Page 33: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

• /* EX08 oracle connection to hr@orcl with oracle_connect */ • /* use execute stmt of proc sql to execute oracle non-select stmts */ • /* e.g. drop and create an oracle table jobs2 in hr@orcl */ • %macro oracle_connect(schema); • proc sql ; • CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); • %mend;

• %macro oracle_select(stmt);

• SELECT * FROM connection to oracle ( • &stmt • );

• %mend;

• %macro oracle_execute(stmt); • execute (&stmt) by oracle; • %mend; • • %oracle_connect(hr); • • %oracle_execute(drop table jobs2); • %oracle_select(select count(*) from jobs); • %oracle_execute(create table jobs2 as select * from jobs);

• quit;

33

Page 34: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracles Examples • 132 /* EX08 oracle connection to hr@orcl with oracle_connect • 132! */ • 133 /* use execute stmt of proc sql to execute oracle non-select • 133! stmts */ • 134 /* e.g. drop and create an oracle table jobs2 in hr@orcl • 134! */ • 135 %macro oracle_connect(schema); • 136 proc sql ; • 137 CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000); • 138 %mend; • 139 • 140 %macro oracle_select(stmt); • 141 • 142 SELECT * FROM connection to oracle ( • 143 &stmt • 144 ); • 145 • 146 %mend; • 147 • 148 %macro oracle_execute(stmt); • 149 execute (&stmt) by oracle; • 150 %mend; • 151 • 152 %oracle_connect(hr); • 153 • 154 %oracle_execute(drop table jobs2); • 155 %oracle_select(select count(*) from jobs); • 156 %oracle_execute(create table jobs2 as select * from jobs); • 157 • 158 quit; • NOTE: PROCEDURE SQL used (Total process time): • real time 1.26 seconds • cpu time 0.01 seconds

34

Page 35: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

35

EX08 output

Page 36: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

• /* EX09 oracle connection to hr@orcl with oracle_connect */ • /* preserve_comments=yes to pass oracle hints to oracle optimizer */ • /* + first_rows enclosed in slash asterik means use the plan with the lowest cost */ • /* e.g. good for select from tables with billions of rows */ • %macro oracle_connect(schema); • proc sql ; • CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000 PRESERVE_COMMENTS=yes); • %mend;

• %macro oracle_select(stmt);

• SELECT * FROM connection to oracle ( • &stmt • );

• %mend;

• %oracle_connect(hr);

• %oracle_select(select /*+ FIRST_ROWS */ * from employees where rownum < 10);

• quit;

36

Page 37: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples 181 /* EX09 oracle connection to hr@orcl with oracle_connect 181! */ 182 /* preserve_comments=yes to pass oracle hints to oracle 182! optimizer */ 183 /* + first_rows enclosed in slash asterik means use the plan 183! with the lowest cost */ 184 /* e.g. good for select from tables with billions of rows 184! */ 185 %macro oracle_connect(schema); 186 proc sql ; 187 CONNECT TO ORACLE (USER=&schema PW=orcl PATH='' BUFFSIZE=20000 187! PRESERVE_COMMENTS=yes); 188 %mend; 189 190 %macro oracle_select(stmt); 191 192 SELECT * FROM connection to oracle ( 193 &stmt 194 ); 195 196 %mend; 197 198 %oracle_connect(hr); 199 200 %oracle_select(select /*+ FIRST_ROWS */ * from employees where 200! rownum < 10); 201 202 quit; NOTE: PROCEDURE SQL used (Total process time): real time 1.03 seconds cpu time 0.04 seconds

37

Page 38: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Oracle Examples

EX09 output

38

Page 39: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

Libname oracle_library_name oracle user=oracle_schema pw=oracle_schema_pw path=oracle database name

<buffsize= dbindex= insertbuff=.....> <schema=> <dbconinit=>;

Data sas_oracle_table;

set oracle_library_name.oracle_schema_table

Run;

Proc sql <outobs=n>;

any sas sql stmts syntax referencing oracle_library_name.oracle_schema_table

Quit;

39

Page 40: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS examples

/*EX10 create an oracle table 1 libname stmt 1 schema */

/*EX11 create oracle table 2 libname stmts 2 schemas */

40

Page 41: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

• /* EX10 oracle connection to hr@orcl via a libname stmt */ • /* libname oralib oracle user= pw= */ • /* proc sql without connect to oracle stmt */ • /* but select from oralib.oracle_table_name */ • /* even create a oracle table */

• %macro oracle_sas_select(oracle_table); • proc sql; • SELECT * FROM oralib.&oracle_table; • quit; • %mend;

• %macro oracle_sas_create_table(new_oracle_table,oracle_table); • proc sql; • drop table oralib.&new_oracle_table; • create table oralib.&new_oracle_table as select * from oralib.&oracle_table; • quit; • %mend;

• * libname oralib clear;

• libname oralib oracle user=hr pw=orcl path='';

• %oracle_sas_select(regions); • %oracle_sas_create_table(regions2,regions); •

41

Page 42: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples 203 /* EX10 oracle connection to hr@orcl via a libname stmt 203! */ 204 /* libname oralib oracle user= pw= 204! */ 205 /* proc sql without connect to oracle stmt 205! */ 206 /* but select from oralib.oracle_table_name 206! */ 207 /* even create a oracle table 207! */ 208 209 210 %macro oracle_sas_select(oracle_table); 211 proc sql; 212 SELECT * FROM oralib.&oracle_table; 213 quit; 214 %mend; 215 216 217 %macro oracle_sas_create_table(new_oracle_table,oracle_table); 218 proc sql; 219 drop table oralib.&new_oracle_table; 220 create table oralib.&new_oracle_table as select * from 220! oralib.&oracle_table; 221 quit; 222 %mend; 223 224 * libname oralib clear; 225 226 libname oralib oracle user=hr pw=XXXX path=''; NOTE: Libref ORALIB was successfully assigned as follows: Engine: ORACLE Physical Name: 227 228 %oracle_sas_select(regions); NOTE: PROCEDURE SQL used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 229 %oracle_sas_create_table(regions2,regions); NOTE: Table ORALIB.REGIONS2 has been dropped. NOTE: SAS variable labels, formats, and lengths are not written to DBMS tables. NOTE: Table ORALIB.REGIONS2 created, with 4 rows and 2 columns. NOTE: PROCEDURE SQL used (Total process time): real time 1.88 seconds cpu time 0.06 seconds

42

Page 43: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

EX10 output

43

Page 44: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

EX10 output

44

Page 45: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

/* EX11 oracle connection to hr@orcl & sh@orcl via 2 libname stmts */ /* libname oralib oracle user=sh pw= schema=hr (set current session to hr) */ /* libname oralib2 oracle user=sh pw= */ /* sh must be granted select on hr.regions first by sys */ /* select from oralib.regions (from hr schema) */ /* create a oracle table regions3 in sh only 3 obs */ * options mprint; %macro oracle_sas_select(oracle_table); proc sql; SELECT * FROM oralib.&oracle_table; quit; %mend; %macro oracle_sas_create_table(new_oracle_table,oracle_table); proc sql outobs=3; drop table oralib2.&new_oracle_table; create table oralib2.&new_oracle_table as select * from oralib.&oracle_table; quit; %mend; libname oralib clear; libname oralib oracle user=sh pw=orcl path='' schema=hr ; /* dbconinit="BEGIN execute immediate 'alter session set current_schema=hr'; END;" ; */ libname oralib2 oracle user=sh pw=orcl path=''; %oracle_sas_select(regions); %oracle_sas_create_table(regions3,regions);

45

Page 46: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples 230 /* EX11 oracle connection to hr@orcl & sh@orcl via 2 libname stmts 230! */ 231 /* libname oralib oracle user=sh pw= schema=hr (set current 231! session to hr) */ 232 /* libname oralib2 oracle user=sh pw= 232! */ 233 /* sh must be granted select on hr.regions first by sys 233! */ 234 /* select from oralib.regions (from hr schema) 234! */ 235 /* create a oracle table regions3 in sh only 3 obs 235! */ 236 237 * options mprint; 238 %macro oracle_sas_select(oracle_table); 239 proc sql; 240 SELECT * FROM oralib.&oracle_table; 241 quit; 242 %mend; 243 244 245 %macro oracle_sas_create_table(new_oracle_table,oracle_table); 246 proc sql outobs=3; 247 drop table oralib2.&new_oracle_table; 248 create table oralib2.&new_oracle_table as select * from 248! oralib.&oracle_table; 249 quit; 250 %mend; 251 252 libname oralib clear; NOTE: Libref ORALIB has been deassigned. 253 254 libname oralib oracle user=sh pw=XXXX path='' schema=hr ; NOTE: Libref ORALIB was successfully assigned as follows: Engine: ORACLE Physical Name: 255 /* dbconinit="BEGIN execute immediate 'alter session set 255! current_schema=hr'; END;" ; */ 256 257 libname oralib2 oracle user=sh pw=XXXX path=''; NOTE: Libref ORALIB2 was successfully assigned as follows: Engine: ORACLE Physical Name: 258 259 %oracle_sas_select(regions); NOTE: PROCEDURE SQL used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 46

Page 47: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

260 %oracle_sas_create_table(regions3,regions); NOTE: Table ORALIB2.REGIONS3 has been dropped. NOTE: SAS variable labels, formats, and lengths are not written to

DBMS tables. WARNING: Statement terminated early due to OUTOBS=3 option. NOTE: Table ORALIB2.REGIONS3 created, with 3 rows and 2 columns. NOTE: PROCEDURE SQL used (Total process time): real time 1.79 seconds cpu time 0.03 seconds

47

Page 48: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

EX11 output

48

Page 49: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

SAS Examples

EX11 output

49

Page 50: 2012 SQL Challenges Between SAS & Oracle Group Presentations... · Oracle Examples All examples done on PC sas 9.1.3 sp4 with an old Oracle 10gR2 for Vista 32 bits Data Base installed

Summary

• Proc SQL very useful procedure in SAS

• Choose between sas syntax or pass through to Oracle syntax

• Use pass through to Oracle syntax when Oracle hints required or if you have code in Oracle syntax

50