如何在SQL Server中不使用循环更新列?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在SQL Server中不使用循环更新列?相关的知识,希望对你有一定的参考价值。
由于性能方面的考虑,我只需要删除循环并使用一些连接或其他解决方案来更新#Result表中的数据并获得相同的循环返回结果。
标量函数:
CREATE FUNCTION [MultiplyerScl]
(@a INT, @b INT)
RETURNS INT
AS
BEGIN
DECLARE @i AS INT = 2
DECLARE @Value AS INT
IF (@b % @a = 0)
BEGIN
SET @Value = @b
END
ELSE
BEGIN
WHILE (1=1)
BEGIN
IF((@b * @i) % @a = 0)
BEGIN
SET @Value = @b * @i
BREAK;
END
ELSE
BEGIN
SET @i = @i + 1
END
END
END
RETURN @Value
END
表设计及其价值。
CREATE TABLE #NUM (Groupid INT, GroupValue INT)
INSERT INTO #NUM
VALUES (1, 8), (1, 9), (1, 23), (2, 5), (2, 5), (2, 10)
主要用于循环逻辑。
SELECT
Groupid,
GroupValue,
MaxValue = MAX(GroupValue) OVER (PARTITION BY Groupid),
MaxCount = COUNT(1) OVER(),
RID = ROW_NUMBER() OVER (ORDER BY groupid)
INTO
#Result
FROM
#NUM
DECLARE @i AS INT = 1
DECLARE @RawCnt AS INT = (SELECT MAX(MaxCount) FROM #Result)
DECLARE @iGroupid INT
DECLARE @iGroupvalue INT
DECLARE @iMaxValue INT
WHILE(@i <= @RawCnt)
BEGIN
SELECT
@iGroupid = Groupid,
@iGroupvalue = Groupvalue,
@iMaxValue = MaxValue
FROM
#Result
WHERE
RID = @i
UPDATE #Result
SET MaxValue = dbo.[MultiplyerScl](@iGroupvalue, @iMaxValue)
WHERE Groupid = @iGroupid
SET @i = @i + 1
END
SELECT * FROM #Result
答案
试试吧
SELECT
Groupid,
GroupValue,
MaxValue = MAX(GroupValue) OVER (PARTITION BY Groupid),
MaxCount = COUNT(1) OVER(),
RID = ROW_NUMBER() OVER (ORDER BY groupid)
INTO
#Result
FROM
#NUM
;WITH Res AS
(
SELECT Groupid, e.Groupvalue, dbo.[MultiplyerScl](Groupvalue, e.MaxValue) AS
MaxValue, 1 AS i
FROM #Result e
UNION ALL
--recursive execution
SELECT e.Groupid, m.Groupvalue, dbo.[MultiplyerScl](e.Groupvalue, m.MaxValue) AS MaxValue, m.i + 1 AS i
FROM #Result e
INNER JOIN Res m ON e.Groupid = m.Groupid
WHERE dbo.[MultiplyerScl](e.Groupvalue, m.MaxValue) > m.MaxValue
)
SELECT Groupid, MAX(MaxValue) AS MaxValue
INTO #FinalResult
FROM Res
GROUP BY Groupid
UPDATE re
SET re.MaxValue = ire.MaxValue
FROM #FinalResult ire
INNER JOIN #Result re ON re.Groupid = ire.Groupid
SELECT * FROM #Result
以上是关于如何在SQL Server中不使用循环更新列?的主要内容,如果未能解决你的问题,请参考以下文章
从 Excel 更新 SQL Server 表,然后填充剩余的列数据
如何在 SQL Server 的单个批次中为列添加默认值并使用默认值更新所有行?
sql语句将Excel中的一列批量更新到sql server中的一列中?