有一个表及数据
------
例子1:
id(int) duanS (char(1))
1 Y
2 Y
3 N
4 Y
例子1:
id(int) duanS (char(1))
1 Y
2 Y
3 N
4 N
--------------------------------------
[email protected](int),[email protected]=2{对应id=2}。
请教一SQL语句,[email protected]:
1.id > @cc
2.最近(方向向下)[email protected]
3.如果不存在(方向向下)的duanS值为“Y”的ID号,[email protected][email protected]+1
--------------------------------------
sql语句的结果:
在例子1中:@cc=4
在例子2中:@cc=3
--
请教sql语句!
------解决方案--------------------
以id是顺序排列理解
declare @cc int
set @cc=2
if exist (select top 1 id from 例子1 where id> @cc order by id )
select select top 1 @cc=id from 例子1 where id> @cc order by id
else
set @[email protected]+1