I have a code which I have already tested and used in the past. I want to automate this code now with an if-statement. Meaning when rows are returned I want to execute the process with the cursor. If no records are found or nothing is returned I don't anything to happen and simply stop.
Attached, is my code how can I embed the if statement correctly?
Thank you very much!
declare @date smalldatetime
set @date = (select max(date) from inventory_table)
select symbol, id, count(distinct 2) cnt into #target
from inventory_table
where date between dateadd(day, -7, @date) and @date
group by symbol,id
having count(distinct 2) >= 4
-- Process with cursor starts here
DECLARE @MyList TABLE (iid int)
INSERT INTO @MyList
select distinct id from #target
DECLARE @iid int
DECLARE db_cursor CURSOR FOR
SELECT iid FROM @MyList
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @iid
WHILE @@FETCH_STATUS = 0
BEGIN
declare @mindate date,
@maxdate date
set @mindate = ( select min(date) from inventory_table where ID = @iid )
set @maxdate = ( select max(date) from inventory_table where ID = @iid )
exec spReissuingIDs @mindate , @maxdate, @iid
FETCH NEXT FROM db_cursor INTO @iid
END
CLOSE db_cursor
DEALLOCATE db_cursor
Aucun commentaire:
Enregistrer un commentaire