BookmarkSubscribeRSS Feed
SASdn
Obsidian | Level 7

Hi all

 

I have the following code that sends email and updates a column to E after the email has been sent.

 

proc sql noprint;
Connect to Greenplm as dbcon (user=&user pw=&pass server=&HPAHost port=5432 database=aae_data);

SELECT * INTO :email_flg
FROM connection to dbcon (
SELECT count(1)::smallint
FROM f2_t1
WHERE rflag = 1 AND (process_flag <> 'E')
);
disconnect from dbcon;
quit;

%let email_body = Hello world;
%let to = aa@ss.com;
%let cc = ab@ss.com
%let subject_text = Test;


%if &email_flg > 0 %then %do;

filename outbox email &to;
data _null_;
file outbox
to=(&to)
cc=(&cc)
subject=&subject_text
;
put &email_body;
run;

%end;

 

proc sql;
Connect to xxxx as dbcon (user=xxx pw=xxx server=xxxx port=xxxx database=aae_data);
execute ( update f2_t1 set process_flag = 'E' where run_flag = 1) by dbcon ;
disconnect from dbcon;
quit;

 

I am updating the process_flag to E once the email for the process has been sent. I create the email_flg to ensure that email doesn't get sent again.


But if the process fails after the email step and before the update statement, the email gets sent twice (or multiple times if the failure keeps occuring after the email step). Is there a way where it is possible to email and do the update in single transaction so that sending email fails if the update fails and vice versa? Or is there any other way to accomplish this scenario in SAS?

Thanks!

1 REPLY 1
Daniel-Santos
Obsidian | Level 7

Since you cannot rollback an email, it makes more sense to me to do the update first and rollback if the message is not sent.

 

proc sql noprint;
Connect to Greenplm as dbcon (user=&user pw=&pass server=&HPAHost port=5432 database=aae_data);
SELECT * INTO :email_flg
FROM connection to dbcon (
SELECT count(1)::smallint
FROM f2_t1
WHERE rflag = 1 AND (process_flag <> 'E')
);
disconnect from dbcon;
quit;

%if &email_flg > 0 %then %do;

* try to update first;
proc sql;
Connect to xxxx as dbcon (user=xxx pw=xxx server=xxxx port=xxxx database=aae_data);
execute ( update f2_t1 set process_flag = 'E' where run_flag = 1) by dbcon ;
disconnect from dbcon;
quit;

* updated?;
%if not &SQLXRC %then %do;

%let email_body = Hello world;
%let to = aa@ss.com;
%let cc = ab@ss.com
%let subject_text = Test;

filename outbox email &to;
data _null_;
file outbox
to=(&to)
cc=(&cc)
subject=&subject_text
;
put &email_body;
run;

* email sent?;
%if &SYSERR %then %do;

* rollback the update;
proc sql;
Connect to xxxx as dbcon (user=xxx pw=xxx server=xxxx port=xxxx database=aae_data);
execute ( <ROLLBACK HERE> ) by dbcon ;
disconnect from dbcon;
quit;

%end;
%end;
%end;

 

I have not tested the code, and you must check how to issue a rollback on your greenplum platform.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 729 views
  • 0 likes
  • 2 in conversation