Question

I am trying to get an insert statement export from a vertica database table. I am trying to create a query that generates insert statements but I am experiencing syntax issues. How can I do something like the below in Vertica?

select "insert into owner.target_tbl values ( "  ||"'"||a.Head_id||"',"||"'"||a.creation_dt||"',"||"'"||a.section"'"||");" as Query_column
from source_tbl a
Was it helpful?

Solution 2

Use single quotes not double quotes:

=> select "foo" || "bar" from dual;
ERROR:  column "foo" does not exist
=> select 'foo' || 'bar' from dual;
 ?column? 
----------
 foobar
(1 row)

OTHER TIPS

If you quote a name with double-quotes, Vertica considers that it is a column, which in your case will lead to an error.

The solution will be to:

  • Use single quotes around your strings
  • If you need a single quote inside your single-quoted string, just double it (twice the single quote, not a double quote):

    select 'insert into owner.target_tbl values (' ||'''' || 'a string' ||''', ' || 2 ||');' as Query_column;
    
                         Query_column                     
    ------------------------------------------------------
    insert into owner.target_tbl values ('a string', 2);
    (1 row)
    

Advice: make sure you add cases to wrap up possible null values- if one of the arguments to concatenation sequence is null- your result will be null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top