Example: Valid Use of Subquery in WHEN Clause - Advanced SQL Engine - Teradata Database

SQL Data Definition Language Syntax and Examples

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
wgr1555383704548.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1144
lifecycle
previous
Product Category
Teradata Vantage™

The following CREATE TRIGGER request succeeds because its WHEN clause aggregate function is specified as part of a subquery:

Note that the aggregate subquery is on the right hand side of the inequality condition, as it must be.

     CREATE TRIGGER trigwhen3
       BEFORE INSERT ON t1
       REFERENCING OLD AS previous 
     FOR EACH ROW 
       WHEN (previous.a <= (
         SELECT SUM(b)
         FROM t2, t3 
         WHERE t2.c = t3.c))
     ABORT;