答案 0 :(得分:0)
尝试使用此代码并多次运行,以便一次只能生成一个,并使其他为假
DECLARE @COUNT INT
SELECT @COUNT=COUNT(1) FROM #tt
SELECT id,custId,IsActive FROM
(
SELECT id,custId, 0 As IsActive FROM #tt where id <=(@COUNT-1)
UNION
SELECT id,custId, 1 As IsActive FROM #tt where id =@COUNT
)DT
ORDER BY NEWID()
如果我们运行查询,则通过给出1值作为tru来随机生成Result,而其他值为false,即0
id custId IsActive
----------------------------
1 2 0
2 2 0
4 2 1
3 2 0
答案 1 :(得分:0)
由于它是一个临时表,您可以更新它并将所需的ID切换为活动状态,同时将当前活动切换为非活动状态。
实施例:
--drop table #temp;
create table #temp (id int, custId int, IsActive int);
insert into #temp (id,custId,IsActive) values (1,2,0),(2,2,0),(3,2,1),(4,2,0);
select * from #temp;
declare @id int;
set @id = 4;
update #temp set IsActive = iif(id=@id,1,0) where (id=@id or IsActive=1);
select * from #temp;