Hello,
For a table having date, instrument & test number, I need to log per instrument, the lowest test, the highest test and any missing tests, for yesterday.
There are a dozen instruments, and an average of the same number for tests per instrument.
So far I have written the following (Does not work for all cases), but am sure there is a simpler way to process an array for each instrument, for yesterday:
>>>
>>>
Is there a way to write this to process an array for each instrument for yesterday? if not, is there a simpler procedure than the logic I am attempting?
Thank you,
Eyal
For a table having date, instrument & test number, I need to log per instrument, the lowest test, the highest test and any missing tests, for yesterday.
There are a dozen instruments, and an average of the same number for tests per instrument.
So far I have written the following (Does not work for all cases), but am sure there is a simpler way to process an array for each instrument, for yesterday:
>>>
Code:
ALTER PROCEDURE dbo.audit_sp AS DECLARE @insrt_String nvarchar(1000) DECLARE @prev_date nvarchar(14) DECLARE @prev_tst nvarchar(4) DECLARE @prev_test decimal(4) DECLARE @curr_date nvarchar(12) DECLARE @curr_instr nvarchar(10) DECLARE @curr_test decimal(4) DECLARE @curr_tst nvarchar(4) DECLARE @next_date nvarchar(12) DECLARE @next_instr nvarchar(10) DECLARE @next_test decimal(4) DECLARE @next_tst nvarchar(4) DECLARE @min_tst nvarchar(4) DECLARE @max_tst nvarchar(4) DECLARE @miss_tst nvarchar(100) DECLARE @ReturnCode int DECLARE curs_get_curr_tst CURSOR FOR SELECT date, instrument, test_number FROM x_all_v WHERE date BETWEEN GETDATE() AND GETDATE()-2 ORDER BY 2,1,3 FOR READ ONLY OPEN curs_get_curr_tst FETCH NEXT FROM curs_get_curr_tst INTO @curr_date, @curr_instr, @curr_tst WHILE @@FETCH_STATUS = 0 BEGIN -- for all records FETCH NEXT FROM curs_get_curr_tst INTO @next_date, @next_instr, @next_tst SET @min_tst =@curr_tst SET @prev_tst =(SELECT MAX(test_number) as prev_test FROM dbo.draeger_all_v WHERE instrument =@curr_instr AND date < '12/30/2004') IF @curr_tst > @prev_tst +1 SET @miss_tst = @curr_tst - 1 a: IF @curr_instr <> @next_instr BEGIN SET @max_tst = @curr_tst SET @insrt_String = 'INSERT INTO [dbo].[audit_log] VALUES ( '''+SUBSTRING(@curr_date,0,12)+''', ''' +@curr_instr+ ''', '+@prev_tst+', '+@min_tst+', '+@max_tst+', '''+ISNULL(@miss_tst,'')+''')' EXEC @ReturnCode = sp_executesql @insrt_String SET @curr_date = @next_date SET @curr_instr= @next_instr SET @curr_tst = @next_tst SET @miss_tst = '' SET @max_tst = '' END IF @curr_instr = @next_instr AND @next_tst = @curr_tst +1 BEGIN SET @curr_date = @next_date SET @curr_instr= @next_instr SET @curr_tst = @next_tst FETCH NEXT FROM curs_get_curr_tst INTO @next_date, @next_instr, @next_tst GOTO a; END IF @curr_instr = @next_instr AND @next_tst <> @curr_tst +1 BEGIN SET @miss_tst = @next_tst - 1 SET @curr_date = @next_date SET @curr_instr= @next_instr SET @curr_tst = @next_tst FETCH NEXT FROM curs_get_curr_tst INTO @next_date, @next_instr, @next_tst GOTO a; END IF @@FETCH_STATUS <> 0 SET @max_tst = @next_tst END CLOSE curs_get_curr_tst DEALLOCATE curs_get_curr_tst SET NOCOUNT OFF
Is there a way to write this to process an array for each instrument for yesterday? if not, is there a simpler procedure than the logic I am attempting?
Thank you,
Eyal
Comment