如何在SQL Server中插入100000行?

时间:2011-10-12 08:52:16

标签: sql sql-server sql-server-2008

INSERT INTO pantscolor_t (procode, color, pic) 
VALUES
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
('74251', 'Black', '511black.jpg'),
('74251', 'OD Green', '511odgreen.jpg'),
..........
..........
..........

INSERT INTO pantscolor_t (procode,color,pic)
VALUES
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
('74251', 'Charcoal', '511charcoal.jpg'),
.............
.............
.............

 INSERT INTO........................
 INSERT INTO........................
 INSERT INTO........................
 INSERT INTO........................

我有100000行,但我的insert语句大于1000行。当我在SSMS中运行SQL语句时,出现错误:

  

INSERT语句中的行值表达式数超过了允许的最大1000行值。

4 个答案:

答案 0 :(得分:18)

创建csv文件(或具有已定义字段分隔符和行分隔符的某个文件)并使用“BULK INSERT”选项将文件加载到数据库。文件可以有100000行;使用批量上传加载大文件不会有任何问题。

http://msdn.microsoft.com/en-us/library/ms188365.aspx

答案 1 :(得分:18)

另一种解决方案是使用带有联合的选择查询。

INSERT INTO pantscolor_t (procode,color,pic)
SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
UNION ALL SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
UNION ALL SELECT '74251', 'Black', '511black.jpg'
UNION ALL SELECT '74251', 'OD Green', '511odgreen.jpg'
--etc....
使用

UNION ALL代替UNION,以便在处理数千条记录时加快查询速度。 UNION ALL允许重复行,而UNION将确保结果集中不存在重复项。对于这种情况,我们不想删除任何可能的重复项,因此使用了UNION ALL

答案 2 :(得分:2)

通过应用以下内容,您不应该有任何错误:

INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251','Black','511black.jpg')

INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'OD Green', '511odgreen.jpg')

INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'Black', '511black.jpg')

INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'OD Green', '511odgreen.jpg')

INSERT INTO pantscolor_t (procode,color,pic) VALUES ('74251', 'Black', '511black.jpg')

...........

我尝试了它并且它有效,当然你可以使用excel轻松地连接值。

答案 3 :(得分:-1)

INSERT mytable (col1, col2, col3, col4, col5, col6)
SELECT * FROM (VALUES
('1502577', '0', '114', 'chodba', 'Praha', 'Praha 1'),
('1503483', '0', 'TVP', 'chodba', 'Praha', 'Praha 2'),
/* ... more than 1000 rows ... */
('1608107', '0', '8', 'sklad', 'Tlumačov', 'Tlumačov'),
('1608107', '0', '9', 'sklad', 'Tlumačov', 'Tlumačov')
) AS temp (col1, col2, col3, col4, col5, col6);