How to use conditional statements to execute (or bypass) chunks of code in Hive/SQL?

How to use conditional statements to execute (or bypass) chunks of code in Hive/SQL?



I know how to use conditional statements in Hive to assign values to variables, for example


CASE WHEN expression = condition1 THEN result1
WHEN expression = condition2 THEN result2
...
ELSE result
END;



Now I would like to use conditional statements to decide whether to execute a chunk of code. When I try to apply the above, it fails. For example,


CASE WHEN (expression = condition1 or expression = condition2)
THEN
CREATE TABLE table1;
CREATE TABLE table2;
CREATE TABLE table3;
END;



I get an error message FAILED: ParseException line 5:0 cannot recognize input near 'CASE' 'WHEN' '('.


FAILED: ParseException line 5:0 cannot recognize input near 'CASE' 'WHEN' '('



Is it possible to use conditional statements to execute/bypass chunks of code?
Thanks!





Hive has multiple inserts functionality, e.g. from init_table insert into first_table select ... insert into second_table select ..., where you can use your conditions.
– serge_k
Aug 21 at 11:34


from init_table insert into first_table select ... insert into second_table select ...





Hi @serge_k, thanks for the idea. I haven't been able to find any doc on this. Could you share a link detailing how to do this please?
– A. Debugne
Aug 21 at 17:27





Do you need to just create tables or insert data when conditions are met as well?
– serge_k
Aug 22 at 7:32





I specify a schema and a location when creating the tables so that they're filled with data at creation. Does that answer your question?
– A. Debugne
Aug 22 at 8:03




2 Answers
2



You cannot use conditional statements in Hive with DDL https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL





Thanks for the information @Harold. And I guess you don't know of a work-around?
– A. Debugne
Aug 22 at 13:26



I suggest you to create empty tables then make multiple inserts, e.g.,


from source_table
insert overwrite
table first_table
select column1, column2
where column1 = 'something'
insert overwrite
table second_table
select column1, column2
where column1 = 'something_else'
;



More info about inserts here, about creating tables here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)