如果选择陈述中没有行(选择前1 ...)
,则此语句会出错update tHomePageWorking
set nColumn = 2,
nPosition = ((select top 1 nPosition
from tHomePageWorking
where nColumn = 2
order by nPosition desc) + 1
)
where nPosition = 1 and nColumn = 1
有没有办法测试此语句的计数,如果没有找到记录,则默认为1?
select top 1 nPosition
from tHomePageWorking
where nColumn = 2
order by nPosition desc
答案 0 :(得分:1)
我认为COALESCE功能会解决您的问题。 它将返回第一个非null参数:
update tHomePageWorking
set nColumn = 2,
nPosition = COALESCE (
((select top 1 nPosition
from tHomePageWorking
where nColumn = 2
order by nPosition desc) + 1
)
,1)
where nPosition = 1 and nColumn = 1
答案 1 :(得分:0)
一种方法是
declare @nposition int
set @nposition=(select top 1 nPosition
from tHomePageWorking
where nColumn = 2
order by nPosition desc)
update tHomePageWorking
set nColumn = 2,
nPosition = (coalesce(@nposition,0) + 1)
where nPosition = 1 and nColumn = 1
答案 2 :(得分:0)
declare @pos int;
select @pos=(select top 1 nPosition
from tHomePageWorking
where nColumn = 2
order by nPosition desc)
select @pos=ISNULL(@pos,0)+1;
update tHomePageWorking
set nColumn = 2,
nPosition = @pos
where nPosition = 1 and nColumn = 1