1

如果 select 语句中没有行,则此语句错误(select top 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
4

3 回答 3

1

我认为 COALESCE 功能将解决您的问题。它将返回第一个非空参数:

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
于 2012-08-10T09:05:35.730 回答
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
于 2012-08-10T09:02:26.950 回答
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
于 2012-08-10T09:05:59.330 回答