开发者

Update with function for value only seems to evaluate the function once

开发者 https://www.devze.com 2023-01-06 17:49 出处:网络
I\'m trying to change all the NULLs in an INT column to an incrementing value using a statement like this:

I'm trying to change all the NULLs in an INT column to an incrementing value using a statement like this:

UPDATE [TableName] SET [ColumnName] = dbo.FunctionName() WHERE [ColumnName] IS NULL

where the function looks like this:

CREATE FUNCTION FunctionName() RETURNS INT AS
BEGIN
    RETURN (select MAX(ColumnName) + 1 from TableName)
END

but the result of this is that all the values that were NULL are now set to the same value (i.e. 1 greater than the max before the statement was called).

Is this the expected behaviour? If so, how would I go about getting the result I'm after?

I'd l开发者_StackOverflow中文版ike to avoid using an auto-increment column as all I really want is uniqueness, the incrementing is just a way of getting there.


Yes, this is by design, it is called Halloween protection. To achieve what you want (that is a one time) use an updatable CTE:

with cte as (
SELECT Column,
ROW_NUMBER() OVER () as rn
FROM Table
WHERE Column IS NULL)
UPDATE t
SET t.Column = m.max + t.rn
FROM cte t
JOIN (
SELECT MAX(Column) as max 
FROM Table) m;
0

精彩评论

暂无评论...
验证码 换一张
取 消