Scenario
I need to update a SQL 2008 database daily via a spreadsheet (the only option available). The format is pretty basic, however there are potentially millions of records. Column1 and Column3 will have many predefined duplicate values, already pulled out into separate tables.
Spreadsheet Sample
Column1 Column2 Column3
Apple 10 Red
Apple 20 Red
Apple 15 Blue
Apple 21 Green
Orange 10 Orange
Orange 7 Orange
Orange 9 Red
Orange 70 Blue
Orange 10 Blue
DB Setup
My database is set up with three separate tables:
//Lookup_Column1
id type
1 Apple
2 Orange
//Lookup_Column3
id type
1 Red
2 Blue
3 Green
4 Orange
//Main - this is what should be inserted, after Column1
//and Column2 are matched to their respective ID's
key Column1 Column2 Column3
1 1 10 1
2 1 20 1
3 1 15 2
4 1 21 3
5 2 10 4
6 2 7 4
7 2 9 1
8 2 70 2
9 2 10 2
Question
How can I write the SQL to insert records that match the information from the lookup tables? How can I go from this:
INSERT INTO Main(Column1, Column2) VALUES ('Apple', 10, 'Red');
To this:
INSERT INTO Main(Column1, Column2) VALUES (1, 10, 1);
//pulled from lookup tables, where Apple = 1 and Red = 1
You could try something like this:
There isn’t any fault-tolerance, but it would work as long as you could parse your spreadsheet values into SELECT statements.