【发布时间】:2016-08-28 19:25:12
【问题描述】:
我们正在使用 SQL Server Management Studio 将批量数据插入到我们的一个数据库表中。目前,我们处于将发送到数据库的数据添加到表中的特定行的位置(这由存储过程控制)。我们发现在操作完成之前发生了超时;在这一点上,我们认为由于 while 循环,操作很慢,但我们不确定如何编写更快的等价物。
-- Insert statements for procedure here
WHILE @i < @nonexistingTblCount
BEGIN
Insert into AlertRanking(MetricInstanceID,GreenThreshold,RedThreshold,AlertTypeID,MaxThreshold,MinThreshold)
VALUES ((select id from @nonexistingTbl order by id OFFSET @i ROWS FETCH NEXT 1 ROWS ONLY), @greenThreshold, @redThreshold, @alertTypeID, @maxThreshold, @minThreshold)
set @id = (SELECT ID FROM AlertRanking
WHERE MetricInstanceID = (select id from @nonexistingTbl order by id OFFSET @i ROWS FETCH NEXT 1 ROWS ONLY)
AND GreenThreshold = @greenThreshold
AND RedThreshold = @redThreshold
AND AlertTypeID = @alertTypeID);
set @i = @i + 1;
END
其中@nonexistingTblCount 是表@nonexistingTbl 内的总行数。 @nonexistingTbl 表是前面声明的,包含我们要添加到表中的所有值。
【问题讨论】:
-
你在哪里使用@id参数?
标签: sql sql-server database while-loop