[英]How to simplify batched SQL Inserts with too many parameters
So I am trying to insert data that looks like this: 所以我试图插入如下所示的数据:
INSERT INTO RELATIONSHIP_CONFIG (USERID, WORKGROUPID, PRIORITY) VALUES
(@userId, @WorkgroupId10, @SmartFeedPriority10),
(@userId, @WorkgroupId11, @SmartFeedPriority11),
(@userId, @WorkgroupId12, @SmartFeedPriority12),
(@userId, @WorkgroupId13, @SmartFeedPriority13);
Which generally is very simple and linear as all inserts happen one after the other and performs fine (I think). 这通常非常简单和线性,因为所有插入一个接一个地发生并且表现良好(我认为)。
The issue is that there is a hard limit with the number of SQL Parameters I am allowed to use- 2100. The upper limit edge case accounts for an insert that is quite a bit above that. 问题是我允许使用的SQL参数数量存在硬限制 - 2100.上限边缘情况考虑了一个比它高一点的插入。
I was thinking about passing the data for WorkgroupId and SmartFeedPriority as a csvs and using a split function to create tables or something like that... 我正在考虑将WorkgroupId和SmartFeedPriority的数据作为csvs传递,并使用split函数创建表格或类似的东西......
What is the best approach for dealing with data like this? 处理这类数据的最佳方法是什么?
Maybe creating a stored procedure, passing the @UserId, @WorkgroupId (CSV), and @SmartFeedPriority (CSV) and having linear, one by one inserts done this way, but I am not too sure how the logic for this will look... 也许创建一个存储过程,传递@UserId,@ WorkgroupId(CSV)和@SmartFeedPriority(CSV)并以这种方式逐个插入线性,但我不太确定这个逻辑将如何看待.. 。
Looking at your question it's a bit difficult to suggest a good approach. 看看你的问题,建议一个好的方法有点困难。 I'm unable to see how and where the source of the data is.
我无法看到数据来源的方式和位置。
I see you mentioned a CSV file. 我看到你提到了一个CSV文件。 You can import data from a CSV file using the below script.
您可以使用以下脚本从CSV文件导入数据。 Once the data is in a table, you can try one of the below examples.
数据在表格中后,您可以尝试以下示例之一。
IF OBJECT_ID('tempdb.dbo.#TempTable', 'U') IS NOT NULL
DROP TABLE #TempTable ;
CREATE TABLE #TempTable
(
[UserID] NVARCHAR(MAX) NULL,
[WorkGroup] NVARCHAR(MAX) NULL,
[SmartFeedPriority] NVARCHAR(MAX) NULL,
)
BULK INSERT #TempTable
FROM ' put your csv file path here '
WITH
(
FIELDTERMINATOR = ',', -- comma delimited
ROWTERMINATOR = '\n',
CODEPAGE = 65001 --'65001'
)
If the data you're trying to insert is from a table, you could try selecting the data into the table you need it to be. 如果您尝试插入的数据来自表格,您可以尝试将数据选择到您需要的表格中。
Example : 示例:
SELECT [UserID], [WorkgroupID], [SmartFeedPriority]
INTO [dbo].[RELATIONSHIP_CONFIG]
FROM [dbo].[SorceTable]
If you would like to take the procedural route you can try the below. 如果您想采用程序路线,可以尝试以下方法。 The below sample would work if the source of your data is in a table and you would like to individually insert each record.
如果您的数据源位于表中并且您希望单独插入每条记录,则下面的示例将起作用。
Example : 示例:
procedure for insert
插入程序
CREATE PROCEDURE [dbo].[CREATE_RELATIONSHIP_CONFIG](@UserId INT, @WorkgroupId INT, @SmartFeedPriority INT)
AS
BEGIN
INSERT INTO RELATIONSHIP_CONFIG (UserId, WorkgroupId, Priority) VALUES
(@userId, @WorkgroupId, @SmartFeedPriority)
END
You can wrap the above procedure in a while loop. 您可以在while循环中包装上述过程。 I've added a example for it below.
我在下面添加了一个例子。
declare @UserId int;
declare @WorkgroupId int;
declare @Priority int;
WHILE EXISTS (SELECT [UserID] FROM #TempTable)
BEGIN
SELECT TOP 1 @UserId=[UserID], @WorkgroupId=[WorkGroup] , @Priority=[SmartFeedPriority] FROM #TempTable
EXEC [dbo].[CREATE_RELATIONSHIP_CONFIG] @UserId, @WorkgroupId, @Priority
DELETE TOP (1)FROM #TempTable
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.