【发布时间】:2011-11-27 17:10:20
【问题描述】:
场景
我需要每天通过电子表格更新 SQL 2008 数据库(唯一可用的选项)。格式非常基本,但可能有数百万条记录。 Column1 和 Column3 将有许多预定义的重复值,已被提取到单独的表中。
电子表格示例
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
数据库设置
我的数据库设置了三个单独的表:
//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
问题
如何编写 SQL 来插入与查找表中的信息匹配的记录?我该怎么做:
INSERT INTO Main(Column1, Column2) VALUES ('Apple', 10, 'Red');
到这里:
INSERT INTO Main(Column1, Column2) VALUES (1, 10, 1);
//pulled from lookup tables, where Apple = 1 and Red = 1
【问题讨论】:
-
您打算如何将电子表格数据导入 sql-server?
标签: sql sql-server sql-server-2008 excel