Skip to content
Advertisement

Generate insert SQL statements from a CSV file

I need to import a csv file into Firebird and I’ve spent a couple of hours trying out some tools and none fit my needs.

The main problem is that all the tools I’ve been trying like EMS Data Import and Firebird Data Wizard expect that my CSV file contains all the information needed by my Table.

I need to write some custom SQL in the insert statement, for example, I have a CSV file with the city name, but as my database already has all the cities in another table (normalized), I need to write a subselect in the insert statement to lookup for the city and write its ID, also I have a stored procedure to cread GUIDS.

My insert statement would be something like this:

INSERT INTO PERSON (ID, NAME, CITY_ID) VALUES((SELECT NEW_GUID FROM CREATE_GUID), :NAME, (SELECT CITY_ID FROM CITY WHERE NAME = :CITY_NAME)

How can I approach this?

Advertisement

Answer

It’s a bit crude – but for one off jobs, I sometimes use Excel.

If you import the CSV file into Excel, you can create a formula which creates an INSERT statement by using string concatenation in the formula. So – if your CSV file has 3 columns that appear in columns A, B, and C in Excel, you could write a formula like…

="INSERT INTO MyTable (Col1, Col2, Col3) VALUES (" & A1 & ", " & B1 & ", " & C1 & ")"

Then you can replicate the formula down all of your rows, and copy, and paste the answer into a text file to run against your database.

Like I say – it’s crude – but it can be quite a ‘quick and dirty’ way of getting a job done!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement