Teradata中的IF ELSE语句

时间:2018-11-28 16:43:04

标签: sql if-statement teradata teradata-sql-assistant

我有两个sql语句SQL 1和SQL2。此外,我想按照以下逻辑运行它们,

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

能否请您提出一个TERADATA SQL代码?

预先感谢!

2 个答案:

答案 0 :(得分:0)

SQL Assistant支持旧的BTEQ语法:

SELECT * 
FROM MY_TABLE
WHERE COL1 > 0;

.IF ACTIVITYCOUNT = 0 THEN .GOTO l2; -- no result, skip SQL 1

SQL 1     ----- if records are present then run sql 1

.EXIT;  -- don't run SQL2

.LABEL l2;

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

答案 1 :(得分:0)

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;

还要记住,activity_count是保留字。