Export Data as Insert Statements

Moderator: NorbertKrupa

Post Reply
User avatar
bud
Newbie
Newbie
Posts: 14
Joined: Fri Oct 11, 2013 1:45 am

Export Data as Insert Statements

Post by bud » Wed Oct 16, 2013 8:54 pm

Hi,

Is it possible to export data from a table into a script in the form of insert statements? This is an nice feature in MySQL. I would like to have backup scripts that I can use to easily reload dimension tables.

Thanks!!!
Bud Anderson

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Export Data as Insert Statements

Post by JimKnicely » Thu Oct 17, 2013 3:31 am

Hi Bud,

There isn't an easy way to export data as insert statements. But you really don't have to do that if you want a backup of the data. You can simply save the data as a csv (where the character is a pipe symbol by default) file and later load it with the copy command.

Check out this link to see if it helps:

http://www.vertica-forums.com/viewtopic.php?f=10&t=150
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
bud
Newbie
Newbie
Posts: 14
Joined: Fri Oct 11, 2013 1:45 am

Re: Export Data as Insert Statements

Post by bud » Thu Oct 17, 2013 1:33 pm

Thank you for responding to my question, Jim. The method of extracting the data into a flat file and then loading it later via the copy command will work for us.

You said "There isn't an easy way to export data as insert statements.", so what's the hard way :?: :lol: :?:
Bud Anderson

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Export Data as Insert Statements

Post by JimKnicely » Thu Oct 17, 2013 9:41 pm

Here is one hard way:

Code: Select all

dbadmin=> SELECT * FROM test.insert_test;
 c1 |  c2   |     c3
----+-------+------------
  3 | Josh  | 2005-01-01
  2 | Jane  | 1998-02-14
  1 | Jim   | 1982-09-19
  4 | Helen | 2012-08-25
(4 rows)

Code: Select all

[dbadmin@vertica01 ~]$ vsql -At -c "SELECT '\! vsql -At -F \"'',''\" -c \"SELECT ''INSERT INTO ' || table_schema || '.' || table_name || ' VALUES ('' c1, *, '');'' c2 FROM ' || table_schema || '.' || table_name || ';\" | sed -e \"s/('',/(/\" | sed \"s/,'')/)/\"' FROM tables WHERE table_schema = 'test' AND table_name = 'insert_test';" | vsql
INSERT INTO test.insert_test VALUES ('2','Jane','1998-02-14');
INSERT INTO test.insert_test VALUES ('3','Josh','2005-01-01');
INSERT INTO test.insert_test VALUES ('1','Jim','1982-09-19');
INSERT INTO test.insert_test VALUES ('4','Helen','2012-08-25');
This is just a super simple example. You'd have to worry about data that has apostrophes in it and deal with those...
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “New to Vertica Database Administration”