July 27, 2018 at 8:13 am
If I use the following sql and there are no numbers in the range, does the fetch still return a row and what will be the value?
declare @last_key_in_range int, @sql nvarchar(200)
set @sql = N'declare curs cursor for select MAX(control_num) from Tbl_Determination where control_num>=' + @control_number_start + ' AND control_num<=' + @control_number_end
execute Sp_executesql @sql
open curs
fetch next from curs into @last_key_in_range
if @@fetch_status <> 0 set @last_key_in_range = -1
close curs
deallocate curs
July 27, 2018 at 8:41 am
You do not need a cursor for this. You can just do the following
select @last_key_in_range = ISNULL(MAX(control_num) , -1)
from Tbl_Determination
where control_num>=CAST(@control_number_start as int) AND control_num<= CAST(@control_number_end as int)
If no records are found, the value will be -1
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply