BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bsas94
Obsidian | Level 7

Hello experts,

 

For example, in order to check whether a table exists or not and then create a new table, I have been doing the following:

 

%IF %SYSFUNC (EXIST (foo.bar)) %THEN %DO;
    PROC SQL NOPRINT;
      DROP TABLE foo.bar;
    QUIT;
  %END;

PROC SQL NOPRINT;     CONNECT TO ORACLE AS ORA (&STRING_CONN.);           execute by ora (                 create table foo.bar as select * from foo.oldtable                  );     DISCONNECT FROM ORA; QUIT; %IF %SYSFUNC (EXIST (foo.bar2)) %THEN %DO;         PROC SQL;                DROP TABLE foo.bar2;         QUIT; %END; PROC SQL NOPRINT;     CONNECT TO ORACLE AS ORA (&STRING_CONN.);           execute by ora (                 create table foo.bar2 as select * from foo.oldtable2                  );     DISCONNECT FROM ORA; QUIT;

 

Is there any way to optimize the amount of connections being opened and use one proc sql?

 

I have been using explicit passthru to create new tables but I haven't found any way to incorporate the IF in a better way so I can group this code in one proc sql.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

If I were doing this, I would assign a libref with a LIBNAME statement and use PROC FEDSQL to do the deed. Explicit pass-through is a one-liner in FedSQL. For example:

libname ora path=&oraclePath schema=foo user=&OraUser pw=OraPW;

proc FedSQL;
/* Implicit pass-through, FORCE ignores errors if table exists */
drop table ora.bar force; 
/* Explicit pass-through by referencing a libref as the alias */
execute (create table foo.bar as select * from foo.oldTable) by ora;
quit;


 

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

2 REPLIES 2
SASJedi
SAS Super FREQ

If I were doing this, I would assign a libref with a LIBNAME statement and use PROC FEDSQL to do the deed. Explicit pass-through is a one-liner in FedSQL. For example:

libname ora path=&oraclePath schema=foo user=&OraUser pw=OraPW;

proc FedSQL;
/* Implicit pass-through, FORCE ignores errors if table exists */
drop table ora.bar force; 
/* Explicit pass-through by referencing a libref as the alias */
execute (create table foo.bar as select * from foo.oldTable) by ora;
quit;


 

Check out my Jedi SAS Tricks for SAS Users
SASKiwi
PROC Star

It would be a lot more efficient just to query an Oracle dictionary table to see if tables exist or not:

libname myoracle oracle noprompt = <Oracle connection string>;

proc sql;
  connect using myoracle;
  select * from connection to myoracle
  (SELECT TABLE_NAME 
FROM ALL_TABLES 
WHERE TABLE_NAME like '%OLDTABLE%'
);
quit; 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 532 views
  • 0 likes
  • 3 in conversation