IF ELSE statement in Teradata

Mayur Mane picture Mayur Mane · Nov 28, 2018 · Viewed 7.4k times · Source

I have two sql statements SQL 1 & SQL 2. Further, I want to run them as per below logic,

SELECT * 
FROM MY_TABLE
WHERE COL1 > 0;

If ACTIVITY_COUNT = 0 THEN RUN SQL 1     ----- if records are present then run sql 1

ELSE 

RUN SQL 2    ----- if records are not present the run sql 2

Could you please suggest a TERADATA SQL CODE ?

Thanks in Advance!

Answer

access_granted picture access_granted · Nov 29, 2018
CREATE PROCEDURE activity_proc(OUT out_res VARCHAR(30))
BEGIN
  DECLARE l_cnt  int;
  SELECT n_activity_count INTO l_cnt FROM My_Table;

  IF l_cnt=0 THEN
    select  'No activity' into out_res;
  ELSE  
    select  'Activity count: ' || cast(l_cnt as varchar(10)) into out_res;
  END IF;   

END;

Also, keep in mind that activity_count is a reserved word.