I would like to know whether the following can be done..
DECLARE @IsDefault bit
SELECT @IsDefault = IsDefault FROM <TABLE> WHERE <CONDITIONS>
UPDATE <TABLE>
SET IsDefault = max(IsDefault,@IsDefault) WHERE <CONDITIONS>
I would like to know whether the following can be done..
DECLARE @IsDefault bit
SELECT @IsDefault = IsDefault FROM <TABLE> WHERE <CONDITIONS>
UPDATE <TABLE>
SET IsDefault = max(IsDefault,@IsDefault) WHERE <CONDITIONS>
The MAX()-function is an aggregate function, and as such you can not use it to compare two values and return only the largest. You have to do something like this instead:
UPDATE <TABLE>
SET IsDefault = CASE WHEN IsDefault > @IsDefault THEN IsDefault ELSE @IsDefault END
WHERE <CONDITIONS>
Similarly for the MIN()-function.
On a side note, if you were pulling this value out into a variable to make things more clear, and could in fact use two sets, this is an interesting way to solve this problem that I've needed before.
SELECT [Other Fields],
(SELECT Max(v)
FROM (VALUES (date1), (date2), (date3),...) AS value(v)) as [MaxDate]
FROM [YourTableName]
It should be noted that this only works on sql server >= 2008.