我是SQL的新手,我正在寻找一种有效的方法将大量数据(准确地600条记录)插入到数据库表中。数据分为4列。
到目前为止,我一直在想这样做
insert into myTable(col1, col2, col3, col4)
values('value 1', 'value 2', 'value 3', 'value 4')
但输入600条记录并不好玩。我怎样才能加快向表中添加值?
答案 0 :(得分:1)
如果这是一次性任务,那么我通常会将数据加载到电子表格中,创建一个简单的公式,将此公式复制到所有行,然后将结果复制粘贴到SQL脚本中 - 并且它&#39完成了。无论是600或100万条记录,工作只需几分钟。
简单示例 - 电子表格如下所示:
+--------+--------+--------+--------+--------------------------------------------------------------------------------------+
| A | B | C | D | E |
+--------+--------+--------+--------+--------------------------------------------------------------------------------------+
| col1 | col2 | col3 | col4 | Formula |
| val 1 | val 21 | val 1 | val 21 | ="INSERT INTO table( col1, col2, col3) VALUES( '"&A2&"','"&B2&"','"&C2&"','"&D2&"');" |
| val 2 | val 22 | val 2 | val 22 | |
| val 3 | val 23 | val 3 | val 23 | |
| val 4 | val 24 | val 4 | val 24 | |
| val 5 | val 25 | val 5 | val 25 | |
| val 6 | val 26 | val 6 | val 26 | |
| val 7 | val 27 | val 7 | val 27 | |
| val 8 | val 28 | val 8 | val 28 | |
| val 9 | val 29 | val 9 | val 29 | |
| val 10 | val 30 | val 10 | val 30 | |
| val 11 | val 31 | val 11 | val 31 | |
| val 12 | val 32 | val 12 | val 32 | |
| val 13 | val 33 | val 13 | val 33 |
.....
.....
.....
E栏中的公式为:
="INSERT INTO table( col1, col2, col3) VALUES( '"&A2&"','"&B2&"','"&C2&"','"&D2&"');"
此公式计算结果如下:
INSERT INTO table( col1, col2, col3) VALUES( 'val 1','val 21','val 1','val 21');
如果将E列中的公式复制到所有行的单元格中,您将获得一个可立即运行的脚本,只需将E列的内容复制到剪贴板并将其粘贴到文本文件中:
INSERT INTO table( col1, col2, col3) VALUES( 'val 1','val 21','val 1','val 21');
INSERT INTO table( col1, col2, col3) VALUES( 'val 2','val 22','val 2','val 22');
INSERT INTO table( col1, col2, col3) VALUES( 'val 3','val 23','val 3','val 23');
INSERT INTO table( col1, col2, col3) VALUES( 'val 4','val 24','val 4','val 24');
INSERT INTO table( col1, col2, col3) VALUES( 'val 5','val 25','val 5','val 25');
INSERT INTO table( col1, col2, col3) VALUES( 'val 6','val 26','val 6','val 26');
INSERT INTO table( col1, col2, col3) VALUES( 'val 7','val 27','val 7','val 27');
INSERT INTO table( col1, col2, col3) VALUES( 'val 8','val 28','val 8','val 28');
INSERT INTO table( col1, col2, col3) VALUES( 'val 9','val 29','val 9','val 29');
INSERT INTO table( col1, col2, col3) VALUES( 'val 10','val 30','val 10','val 30');
INSERT INTO table( col1, col2, col3) VALUES( 'val 11','val 31','val 11','val 31');
INSERT INTO table( col1, col2, col3) VALUES( 'val 12','val 32','val 12','val 32');
INSERT INTO table( col1, col2, col3) VALUES( 'val 13','val 33','val 13','val 33');
INSERT INTO table( col1, col2, col3) VALUES( 'val 14','val 34','val 14','val 34');
INSERT INTO table( col1, col2, col3) VALUES( 'val 15','val 35','val 15','val 35');
INSERT INTO table( col1, col2, col3) VALUES( 'val 16','val 36','val 16','val 36');
...
...
...