There is probably no straightforward solution to generate insert or delete SQL script using csvsql; at least I could not find one myself when I needed it desperately. However, the --query
option comes to our rescue by allowing us to prepare such statements ourselves. Of course, it needs a little bit of effort, but it pays off very well.
Here is an example. I have a csv file (called test2.csv). This is how I am able to generate insert statements for all rows in the csv file.
csvsql --query "SELECT 'insert into test2 values (''' ||
trim(ifnull(my_table.survived, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.RECORD_TYPE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.BASE_HIN, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.SUFFIX, 'null123')) ||''',''' ||
trim(ifnull(my_table.name, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.ADDRESS_1, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.ADDRESS_2, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.CITY, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.STATE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.PRIMARY_ZIP, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.COUNTRY, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.TELEPHONE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.CLASS_OF_TRADE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.DEA, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.DATE_HIN_ASSIGNED, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.DATE_LAST_EDITED, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.STATUS, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.VERIFIED, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.LAST_ACTION_CODE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.REASON_CODE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.REFERBACK_CODE, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.SUBSET, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.UPIN, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.SPECIALTY, 'null123')) ||''' ,''' ||
trim(ifnull(my_table.FAX, 'null123')) ||''' )'FROM test2 as my_table WHERE survived='0'" test2.csv > insert.sql
Make sure that this is one single line because csvsql does not like broken lines.
Also, once the insert.sql
is prepared, you need to remove the header line, remove double quotes (from all the rows) and also find and replace = 'null123'
with is null
.