DATA Step, Macro, Functions and more

Update and email in same step

Reply
Contributor
Posts: 20

Update and email in same step

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!

Contributor
Posts: 24

Re: Update and email in same step

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

Ask a Question
Discussion stats
  • 1 reply
  • 117 views
  • 0 likes
  • 2 in conversation