Converting a spreadsheet to INSERT statements
It is not uncommon to get data in the form of an spreadsheet, typically Excel. This exercise illustrates how to create INSERT statements for loading these data into a relational database.
- Download the spreadsheet containing the shr table in Excel format.
- The goal is to create an INSERT statement for each data row of the spreadsheet. For the first data row of the example spreadsheet, the required SQL is
insert into shr values ('FC','Freedonia Copper',27.5,10529,1.84,16);
- It is possible to construct the INSERT with a single concatenate function, but that is an error prone approach. It is easier to create each of the pieces of the INSERT, and then put them together.
- Create the entry for shrcode, a character string, by entering in cell G2 the formula
which should result in 'FC', (the single quotes have been highlighted so it is easier to see them within the double quotes
- Repeat the process of step 4 to generate 'Freedonia Copper', in H2.
- Create the entry for shrprice, a numeric, by entering in I2 the formula
which should result in 27.5,
- Repeat the process of step 6 to generate 10529, in J2.
- Repeat the process of step 6 to generate 1.84, in K2.
- Repeat the process of step 6 to generate 16 in L2.
- Now put all the pieces together into a single INSERT, with
=CONCATENATE("insert into shr values (",G2,H2,I2,J2,K2,L2,");") in M2.
- Use copy down to generate the remaining INSERT statements.
- Create a table and verify that the INSERT statements work.
Create INSERT statements for the spreadsheet containing the nation table.
This page is part of the promotional and support material for Data Management (sixth edition) by Richard T. Watson
For questions and comments please contact the author