Teradata comparison between 2 variables inside BTEQ and choose an outcome

120 views Asked by At

I need help to compare 2 variables inside Teradata BTEQ script and take a decision based on the outcome. I tried something like below:

 CREATE VOLATILE TABLE VT_JRNL_MAX_SNAPSHOT_DT,NO LOG,NO FALLBACK
 (
 MAX_DATE_JRNL DATE FORMAT 'YYYY-MM-DD'
 );

 INSERT INTO VT_JRNL_MAX_SNAPSHOT_DT
 (
 MAX_DATE_JRNL
 )
 SELECT MAX(JOURNAL_SNAPSHOT_DATE) FROM NDW_NRDP_TABLES.NRDP_EWFM_AGENT_SEGMENT_JRNL;

 CREATE VOLATILE TABLE VT_SEM_MAX_SNAPSHOT_DT,NO LOG,NO FALLBACK
 (
 MAX_DATE_SEM DATE FORMAT 'YYYY-MM-DD'
 );

 INSERT INTO VT_SEM_MAX_SNAPSHOT_DT
 (
 MAX_DATE_SEM
 )
 SELECT MAX(JOURNAL_SNAPSHOT_DATE) FROM NDW_NRDP_TABLES.NRDP_EWFM_AGENT_SEGMENT;

.IF MAX_DATE_JRNL >= MAX_DATE_SEM THEN .GoTo DEL_STEP ;   

.IF MAX_DATE_JRNL < MAX_DATE_SEM THEN .GoTo EXIT_STEP ; 

.LABEL DEL_STEP ;
DEL FROM SEMANTIC;
.LABEL EXIT_STEP ;
INSERT ......;

Thanks, Debasis

1

There are 1 answers

1
xenodevil On

An easy variable in BTEQ which can be helpful here is ACTIVITYCOUNT

With this, you can formulate a query like:

SELECT       1
FROM         VT_SEM_MAX_SNAPSHOT_DT  S
CROSS JOIN   VT_JRNL_MAX_SNAPSHOT_DT J
WHERE        MAX_DATE_JRNL >= MAX_DATE_SEM
;

.IF ACTIVITYCOUNT < 1 THEN .GOTO EXIT_STEP;

DEL FROM SEMANTIC;

.LABEL EXIT_STEP;
...

There may be more neat ways to write this query to avoid cross join and even avoid the volatile tables entirely. But this should serve as a starting point.