Question

I am facing a strange issue while firing an SQL with CASE WHEN statements from within shell.

My SQL looks like:

insert into myschema.myTable (col1, col2, col3) 
select col1, 
         CASE 
            WHEN col2 = 'NULL' THEN NULL 
            ELSE col2 
         END, 
         col3 
from myschema.myTable_ext_table

(It is loading data from an external table that has some NULL values extracted as 'NULL' strings, and we cannot change the external table design)

My objective is to run psql with nohup so that big data loading tasks can run in the background. My psql command looks like this:

nohup  sh -c 'date;psql -h myHAWQHost -d myHAWQDB --command "insert into myschema.myTable (col1, col2, col3) select col1, CASE WHEN col2 = 'NULL' THEN NULL ELSE col2 END, col3 from myschema.myTable_ext_table";date;' > myLog.out &

When I fire the SQL from any client (pgAdmin/Aginity) it works well. However when running from the psql command, it does not work, as the CASE WHEN is still putting 'NULL' strings in col2 column.

Was it helpful?

Solution 2

You have a quoting problem. In order to embed a single quote (') into a shell string which is also surrounded in single quotes, you need to switch to a double quoted string:

change:

cmd 'foo "bar 'baz' quux"'
#             ^   ^

to:

cmd 'foo "bar '"'"'baz'"'"' quux"'
#             ^^^^^   ^^^^^

You can use other escape styles, but this one is portable across all posix shells.

OTHER TIPS

In PostgreSQL you can use a own string limiters:

postgres=# select $$AHOJ$$;
 ?column? 
----------
 AHOJ
(1 row)

These literal separators can use from shel

[pavel@localhost ~]$ sh -c 'psql postgres -c "select \$\$NULL\$\$"'
 ?column? 
----------
 NULL
(1 row)

[pavel@localhost ~]$ sh -c 'psql postgres -c "select \$\$$USER\$\$"'
 ?column? 
----------
 pavel
(1 row)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top