IF
Translation reference to convert Teradata IF statement to Snowflake Scripting
Some parts in the output code are omitted for clarity reasons.
Description
Provides conditional execution based on the truth value of a condition.
For more information regarding Teradata IF, check here.
IF conditional_expression THEN
statement
[ statement ]...
[ ELSEIF conditional_expression THEN
statement
[ statement ]... ]...
[ ELSE
statement
[ statement ]... ]
END IF;
Sample Source Patterns
Sample auxiliar table
IN -> Teradata_01.sql
CREATE TABLE if_table(col1 varchar(30));
OUT -> Teradata_01.sql
CREATE TABLE if_table (
col1 varchar(30))
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"teradata"}}'
;
Possible IF variations
Teradata
IN -> Teradata_02.sql
CREATE PROCEDURE ifExample1 ( flag NUMBER )
BEGIN
IF flag = 1 THEN
INSERT INTO if_table(col1) VALUES ('one');
END IF;
END;
CALL ifExample1(1);
SELECT * FROM if_table;
IN -> Teradata_03.sql
CREATE PROCEDURE ifExample2 ( flag NUMBER )
BEGIN
IF flag = 1 THEN
INSERT INTO if_table(col1) VALUES ('one');
ELSE
INSERT INTO if_table(col1) VALUES ('Unexpected input.');
END IF;
END;
CALL ifExample2(2);
SELECT * FROM if_table;
IN -> Teradata_04.sql
CREATE PROCEDURE ifExample3 ( flag NUMBER )
BEGIN
IF flag = 1 THEN
INSERT INTO if_table(col1) VALUES ('one');
ELSEIF flag = 2 THEN
INSERT INTO if_table(col1) VALUES ('two');
ELSEIF flag = 3 THEN
INSERT INTO if_table(col1) VALUES ('three');
END IF;
END;
CALL ifExample3(3);
SELECT * FROM if_table;
IN -> Teradata_05.sql
CREATE PROCEDURE ifExample4 ( flag NUMBER )
BEGIN
IF flag = 1 THEN
INSERT INTO if_table(col1) VALUES ('one');
ELSEIF flag = 2 THEN
INSERT INTO if_table(col1) VALUES ('two');
ELSEIF flag = 3 THEN
INSERT INTO if_table(col1) VALUES ('three');
ELSE
INSERT INTO if_table(col1) VALUES ('Unexpected input.');
END IF;
END;
CALL ifExample4(4);
SELECT * FROM if_table;
|COL1|
|----|
|one |
|COL1 |
|-----------------|
|Unexpected input.|
|COL1 |
|-----|
|three|
|COL1 |
|-----------------|
|Unexpected input.|
Snowflake Scripting
OUT -> Teradata_02.sql
CREATE OR REPLACE PROCEDURE ifExample1 (FLAG FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"teradata"}}'
EXECUTE AS CALLER
AS
$$
BEGIN
IF (flag = 1) THEN
INSERT INTO if_table (col1)
VALUES ('one');
END IF;
END;
$$;
CALL ifExample1(1);
SELECT
* FROM
if_table;
OUT -> Teradata_03.sql
CREATE OR REPLACE PROCEDURE ifExample2 (FLAG FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"teradata"}}'
EXECUTE AS CALLER
AS
$$
BEGIN
IF (flag = 1) THEN
INSERT INTO if_table (col1)
VALUES ('one');
ELSE
INSERT INTO if_table (col1)
VALUES ('Unexpected input.');
END IF;
END;
$$;
CALL ifExample2(2);
SELECT
* FROM
if_table;
OUT -> Teradata_04.sql
CREATE OR REPLACE PROCEDURE ifExample3 (FLAG FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"teradata"}}'
EXECUTE AS CALLER
AS
$$
BEGIN
IF (flag = 1) THEN
INSERT INTO if_table (col1)
VALUES ('one');
ELSEIF (flag = 2) THEN
INSERT INTO if_table (col1)
VALUES ('two');
ELSEIF (flag = 3) THEN
INSERT INTO if_table (col1)
VALUES ('three');
END IF;
END;
$$;
CALL ifExample3(3);
SELECT
* FROM
if_table;
OUT -> Teradata_05.sql
CREATE OR REPLACE PROCEDURE ifExample4 (FLAG FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
COMMENT = '{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"teradata"}}'
EXECUTE AS CALLER
AS
$$
BEGIN
IF (flag = 1) THEN
INSERT INTO if_table (col1)
VALUES ('one');
ELSEIF (flag = 2) THEN
INSERT INTO if_table (col1)
VALUES ('two');
ELSEIF (flag = 3) THEN
INSERT INTO if_table (col1)
VALUES ('three');
ELSE
INSERT INTO if_table (col1)
VALUES ('Unexpected input.');
END IF;
END;
$$;
CALL ifExample4(4);
SELECT
* FROM
if_table;
|COL1|
|----|
|one |
|COL1 |
|-----------------|
|Unexpected input.|
|COL1 |
|-----|
|three|
|COL1 |
|-----------------|
|Unexpected input.|
Last updated