postgresql - Can't use an IF ... THEN ... ELSE inside my CASE statment SQL state: 42601 -
i building insert query on fly. here collecting column names. columns need scrambled, , need decrypted, scrambled , encrypted.
my idea check if column name in provided array, , if is, check see if value start 'pi2'. if so, apply necessary transformation decrypt, translate, encrypt. else translate value. here query:
select case when column_name = any(array['a', 'pi2_b', 'pi2_c', 'd', 'e']) ( if (substring(column_name 0 4) = 'pi2') 'my_function.my_encrypt(translate(my_function.my_decrypt('||column_name ||'), '''|| var_scramble_key1 ||''','''|| var_scramble_key2 ||'''))'; else 'translate('||column_name||','''|| var_scramble_key1 ||''','''|| var_scramble_key2 ||''')'; end if; ) else column_name end information_schema.columns table_schema = 'my_temp' , table_name = 'table_a' order ordinal_position asc
running query gives me syntax error:
at or near 'then' sql state: 42601 character: 177.
my question why can't use if else
inside case
statement.
i have workaround:
select case when column_name = any(array['a', 'pi2_b', 'pi2_c', 'd', 'e']) , substring(column_name 0 4) = 'pi2' 'my_function.my_encrypt(translate(my_function.my_decrypt('||column_name ||'), '''|| var_scramble_key1 ||''','''|| var_scramble_key2 ||'''))'; when column_name = any(array['a', 'pi2_b', 'pi2_c', 'd', 'e']) 'translate('||column_name||','''|| var_scramble_key1 ||''','''|| var_scramble_key2 ||''')'; else column_name end information_schema.columns table_schema = 'my_temp' , table_name = 'table_a' order ordinal_position asc
because if
not sql key word.
key word if
valid in pl/pgsql not within sql statement.
case
happens valid in both (but there subtle syntax differences!)
Comments
Post a Comment