BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mary001
Fluorite | Level 6

Hi All,

 

I'm a newbie and I need to run a query against 400+ tables in a DB2 database to capture the max last update. Is it possible to have multiple "select" statements using 1 DB2 connection?

 

Also, how can get the results in a form of a list, for example:

table11/1/2021
table21/2/2021
table31/3/2021
table41/4/2021
table51/5/2021

 

Here's the program and when I run it, it only returns the result from the 1st select statement. No errors in the log.

 

%udb(DB2P)
SELECT * FROM CONNECTION TO DB2(
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_ONE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_TWO_DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_THREE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_THREE;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_FOUR_DT_LST_UPDT FROM DB2PROD.CD_EVNT_FOUR;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_FIVE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_FIVE;
); quit;

 

Thank you so much in advance!

 

Mary

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You just want to print the results into the output window? You don't want to store them somewhere?

 

Either run each query separately.

SELECT * FROM CONNECTION TO DB2(
  SELECT MAX(DT_LST_UPDT) AS CD_EVNT_ONE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
);
SELECT * FROM CONNECTION TO DB2(
  SELECT MAX(DT_LST_UPDT) AS CD_EVNT_TWO_DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
);
...

Or tell the remote DB you want it run ONE query that returns all of the results: (Note every implementation of SQL has its own quirks so syntax might need some tweaking). In which case you probably need to add a second variable to tell where the 

SELECT * FROM CONNECTION TO DB2(
  SELECT 'DB2PROD.CD_EVNT_ONE' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
  union
  SELECT 'DB2PROD.CD_EVNT_TWO' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
  ...
);

But you probably want to make a TABLE and not just print the results. 

create table want as 
SELECT * FROM CONNECTION TO DB2(
  SELECT 'DB2PROD.CD_EVNT_ONE' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
  union
  SELECT 'DB2PROD.CD_EVNT_TWO' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
  ...
);

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

i would create a libname, and not use passthrough here. 

Your query is simple enough that implicit pass-through can be used with no adverse effect.

libname DB2PROD   ....   ;
proc sql;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_ONE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_TWO_DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_THREE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_THREE;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_FOUR_DT_LST_UPDT FROM DB2PROD.CD_EVNT_FOUR;
SELECT MAX(DT_LST_UPDT) AS CD_EVNT_FIVE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_FIVE;
quit;
Tom
Super User Tom
Super User

You just want to print the results into the output window? You don't want to store them somewhere?

 

Either run each query separately.

SELECT * FROM CONNECTION TO DB2(
  SELECT MAX(DT_LST_UPDT) AS CD_EVNT_ONE_DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
);
SELECT * FROM CONNECTION TO DB2(
  SELECT MAX(DT_LST_UPDT) AS CD_EVNT_TWO_DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
);
...

Or tell the remote DB you want it run ONE query that returns all of the results: (Note every implementation of SQL has its own quirks so syntax might need some tweaking). In which case you probably need to add a second variable to tell where the 

SELECT * FROM CONNECTION TO DB2(
  SELECT 'DB2PROD.CD_EVNT_ONE' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
  union
  SELECT 'DB2PROD.CD_EVNT_TWO' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
  ...
);

But you probably want to make a TABLE and not just print the results. 

create table want as 
SELECT * FROM CONNECTION TO DB2(
  SELECT 'DB2PROD.CD_EVNT_ONE' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_ONE
  union
  SELECT 'DB2PROD.CD_EVNT_TWO' as source,MAX(DT_LST_UPDT) AS DT_LST_UPDT FROM DB2PROD.CD_EVNT_TWO
  ...
);
Mary001
Fluorite | Level 6

THANK YOU SO MUCH, Tom and Chris!!! You guys are a lifesaver!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1022 views
  • 0 likes
  • 3 in conversation