0

I wanted to export table data in vertica and generates a SQL INSERT script. I exported table schemas and generated a SQL script. Is there any way to export table data in vertica?

Thank you.

Kedar H
  • 41
  • 2
  • 9
  • https://www.vertica.com/kb/Understanding-Vertica-Import-and-Export/Content/BestPractices/Understanding-Vertica-Import-and-Export.htm – IAmJersh Feb 05 '20 at 08:04
  • What have you tried so far? – IAmJersh Feb 05 '20 at 08:04
  • I want to create a SQL's CREATE TABLE of table and INSERT statement of all data in the table and that will use during my project's deployment / installation. I generated a CREATE TABLE statement using vertica's EXPORT_TABLE, and looking to EXPORT data. – Kedar H Feb 05 '20 at 08:12

1 Answers1

1

What you can do is to write a vsql script that puts its output to file.

Script exp.sql:

-- don't align
\a
-- tuples only
\t
\pset fieldsep '|'
-- write to workfile.sql
\o workfile.sql
-- get the create table statement into the workfile

SELECT EXPORT_OBJECTS('','public.foo',FALSE);
-- put the COPY command for in-line data into the workfile
SELECT 'COPY public.foo FROM STDIN DELIMITER ''|'';';
-- export the table's data
SELECT * FROM public.foo;
-- append a backslash-dot line to mark the end of the input of the copy command.
SELECT '\.'; 

Resulting workfile.sql:

CREATE TABLE public.foo
(
    id numeric(37,15),
    first_name varchar(256),
    last_name varchar(256),
    hire_dt timestamp
);


CREATE PROJECTION public.foo
(
 id,
 first_name,
 last_name,
 hire_dt
)
AS
 SELECT foo.id,
        foo.first_name,
        foo.last_name,
        foo.hire_dt
 FROM public.foo
 ORDER BY foo.id,
          foo.first_name,
          foo.last_name,
          foo.hire_dt
UNSEGMENTED ALL NODES;

COPY public.foo FROM STDIN DELIMITER '|';
1.000000000000000|Arthur|Dent|2017-02-05 00:00:00
2.000000000000000|Ford|Prefect|2017-02-05 00:00:00
3.000000000000000|Zaphod|Beeblebrox|2017-02-05 00:00:00
4.000000000000000|Tricia|McMillan|2017-02-05 00:00:00
 [ . . . ]
41.000000000000000|Lunkwill|Lunkwill|2017-02-05 00:00:00
42.000000000000000|Fook|Fook|2017-02-05 00:00:00
\.
marcothesane
  • 6,192
  • 1
  • 11
  • 21