无法将超过 1K 条记录插入 SQL 中的临时 table
Not able to INSERT more than 1K records to temp table in SQL
我是SQL的新手,请帮助我。
我正在尝试将超过 1k 条记录插入到 SQL temp table,我收到此错误:
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values
请帮助我如何解决将记录插入临时文件的问题table。我需要插入5k条记录,请帮助我
这是我的脚本
use DatabaseName
SET NOCOUNT ON
Create Table TempRefundDetails (PolicyNumber NVARCHAR(10))
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES
(1),
(2),
(3)
.
.
.
.
(999),
(1000),
(1001),
(1002)
.
.
你最多只能用一个 VALUES()
结构做 1'000 - 如果你需要更多,你需要重复 INSERT
语句:
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (1), ....., (1000);
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (1001), ....., (2000);
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (2001), ....., (3000);
等等
根据您插入的实际值的来源,可以更改 what/how 以执行 INSERT 部分。您是从单个连接进行插入还是打开和关闭连接以使用数据?这也会改变您的操作方式。
您正在使用 table 变量(名称前面的 @ 表示)。请参阅下面我的注释和代码,了解错误所在以及如何修复它。
use DatabaseName
SET NOCOUNT ON
-- this is creating permeant table not a temp table or table variable.
CREATE Table TempRefundDetails (PolicyNumber NVARCHAR(10))
-- this inserts into a table variable not temp table
INSERT INTO @TempRefundDetails (PolicyNumber)
-- this will create a temp table
IF OBJECT_ID('tempdb..#TempRefundDetails') IS NOT NULL
DROP TABLE #TempRefundDetails
CREATE TABLE #TempRefundDetails (
PolicyNumber NVARCHAR(10)
)
INSERT INTO #TempRefundDetails (
PolicyNumber
)
VALUES (), () -- and so on
OR select from another table source directly into the table
INSERT INTO #TempRefundDetails (
PolicyNumber
)
Select PolicyNumber
From SomeTableNameHere
The documentation is explicit 关于此限制,并建议多种解决方法:
When used as the VALUES clause of an INSERT ... VALUES statement, there is a limit of 1000 rows. Error 10738 is returned if the number of rows exceeds the maximum. 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, the .NET SqlBulkCopy class, OPENROWSET (BULK ...), or the BULK INSERT statement
代码的目的不明确,但是可以使用WHILE LOOP插入记录
DECLARE @TempRefundDetails TABLE (PolicyNumber NVARCHAR(10))
DECLARE @cnt INT = 1;
WHILE @cnt < 5001 <-- your number here
BEGIN
INSERT INTO @TempRefundDetails (PolicyNumber) VALUES (@cnt)
SET @cnt = @cnt + 1
END
SELECT * FROM @TempRefundDetails
我是SQL的新手,请帮助我。
我正在尝试将超过 1k 条记录插入到 SQL temp table,我收到此错误:
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values
请帮助我如何解决将记录插入临时文件的问题table。我需要插入5k条记录,请帮助我
这是我的脚本
use DatabaseName
SET NOCOUNT ON
Create Table TempRefundDetails (PolicyNumber NVARCHAR(10))
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES
(1),
(2),
(3)
.
.
.
.
(999),
(1000),
(1001),
(1002)
.
.
你最多只能用一个 VALUES()
结构做 1'000 - 如果你需要更多,你需要重复 INSERT
语句:
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (1), ....., (1000);
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (1001), ....., (2000);
INSERT INTO @TempRefundDetails (PolicyNumber)
VALUES (2001), ....., (3000);
等等
根据您插入的实际值的来源,可以更改 what/how 以执行 INSERT 部分。您是从单个连接进行插入还是打开和关闭连接以使用数据?这也会改变您的操作方式。
您正在使用 table 变量(名称前面的 @ 表示)。请参阅下面我的注释和代码,了解错误所在以及如何修复它。
use DatabaseName
SET NOCOUNT ON
-- this is creating permeant table not a temp table or table variable.
CREATE Table TempRefundDetails (PolicyNumber NVARCHAR(10))
-- this inserts into a table variable not temp table
INSERT INTO @TempRefundDetails (PolicyNumber)
-- this will create a temp table
IF OBJECT_ID('tempdb..#TempRefundDetails') IS NOT NULL
DROP TABLE #TempRefundDetails
CREATE TABLE #TempRefundDetails (
PolicyNumber NVARCHAR(10)
)
INSERT INTO #TempRefundDetails (
PolicyNumber
)
VALUES (), () -- and so on
OR select from another table source directly into the table
INSERT INTO #TempRefundDetails (
PolicyNumber
)
Select PolicyNumber
From SomeTableNameHere
The documentation is explicit 关于此限制,并建议多种解决方法:
When used as the VALUES clause of an INSERT ... VALUES statement, there is a limit of 1000 rows. Error 10738 is returned if the number of rows exceeds the maximum. 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, the .NET SqlBulkCopy class, OPENROWSET (BULK ...), or the BULK INSERT statement
代码的目的不明确,但是可以使用WHILE LOOP插入记录
DECLARE @TempRefundDetails TABLE (PolicyNumber NVARCHAR(10))
DECLARE @cnt INT = 1;
WHILE @cnt < 5001 <-- your number here
BEGIN
INSERT INTO @TempRefundDetails (PolicyNumber) VALUES (@cnt)
SET @cnt = @cnt + 1
END
SELECT * FROM @TempRefundDetails