IF..THEN statement in SQL (not CASE)

Database
The Teradata Database channel includes discussions around advanced Teradata features such as high-performance parallel database technology, the optimizer, mixed workload management solutions, and other related technologies.

IF..THEN statement in SQL (not CASE)

Hi

 

I am trying to mimic 'IF..then' statement in SQL like below.. Can you please advise if this is possible without using procedures/B-teq Scripts?

 

If table A is empty

    display 'No data' 

else

     Select * from table A

 

Thanks in advance.

Tags (2)

Accepted Solutions
Ambassador

Re: IF..THEN statement in SQL (not CASE)

Hi,

 

Not really. I think the closest that you will get is something like the following:

select *
from tablea
union all
select 'no data' as col1
 ,other 'dummy' columns as required
from dbc.databasesv
where databasename = user
  and 0 = (select count(*) from tablea);

(and yes, it's a bit 'clunky')

 

Because these two SELECT statements are connected via a set operator you will have to make sure that the number of columns in each select list are the same and that corresponding columns (the 1st in each, the 2nd in each etc.) have the compatible data types.

 

HTH

Dave

Ward Analytics Ltd - information in motion
www: http://www.ward-analytics.com
1 ACCEPTED SOLUTION
2 REPLIES 2
Ambassador

Re: IF..THEN statement in SQL (not CASE)

Hi,

 

Not really. I think the closest that you will get is something like the following:

select *
from tablea
union all
select 'no data' as col1
 ,other 'dummy' columns as required
from dbc.databasesv
where databasename = user
  and 0 = (select count(*) from tablea);

(and yes, it's a bit 'clunky')

 

Because these two SELECT statements are connected via a set operator you will have to make sure that the number of columns in each select list are the same and that corresponding columns (the 1st in each, the 2nd in each etc.) have the compatible data types.

 

HTH

Dave

Ward Analytics Ltd - information in motion
www: http://www.ward-analytics.com

Re: IF..THEN statement in SQL (not CASE)

Thank you, Dave