Temporary Table - Maximum allowed number of 1000 row values

∥☆過路亽.° 提交于 2019-12-07 06:13:35

问题


When trying to insert 6000 rows into a temp table I get the following message

The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.

Source is not located in SQL Server.

CREATE TABLE #TMP_ISIN (
   [Isin] nVARCHAR(250))

INSERT INTO #TMP_ISIN ([Isin])
VALUES
ABOUT 6000 ROWS

How shall I do to avoid this limit?


回答1:


The limit of 1000 is on the number of rows in the values clause of the insert rather than a limitation of the temporary table itself:

The maximum number of rows that can be constructed by inserting rows directly in the VALUES list is 1000. Error 10738 is returned if the number of rows exceeds 1000 in that case.

To insert more than 1000 rows, use one of the following methods:

  • Create multiple INSERT statements;
  • Use a derived table;
  • Bulk import the data by using the bcp utility or the BULK INSERT statement.

Hence you can do it in chunks, with smaller insert statements.

insert into sometable (somecolumns) values <about 1000 rows>;
insert into sometable (somecolumns) values <about 1000 rows>;
:
insert into sometable (somecolumns) values <about 1000 rows>;

If you need all 6000 to be atomic, you can put a transaction around the whole thing.




回答2:


It's only an issue with the VALUES clause. If you're inserting more than 1000 rows by this method (questionable approach but hey) then use a set of SELECT statements with UNION ALL:

INSERT #a (a,b)
SELECT 'a', 'b' UNION ALL
SELECT 'c', 'd' UNION ALL
SELECT ...etc

This also has the advantage that you can check the resultset from the SELECT statements prior to doing the INSERTs - you won't get that luxury with the VALUES construct.



来源:https://stackoverflow.com/questions/30319998/temporary-table-maximum-allowed-number-of-1000-row-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!