I am tasked to create a stored procedure for a publisher application to retrieve employee data from our SQL Server.
The aim: We have EmpTable (lets call this the source). We also have EmpData table. It has a column called 'Status' with a default value: 'UNPROCESSED'. Our aim is to create a SP(lets call this SP1) so that: it polls all the data from the EmpTable in batches of 100 rows. It should then continue polling data from this table until SP returns zero records. Once the above completed processing, another SP(lets call this SP2) is created/called to change the status to 'COMPLETED'. If the record processing is failed permanently in the app, that is due to errors such as schema or validation errors (none retryable), SP2 to change the status to FAILED. The results is then populated in the EmpData table We aim to run this batch job once a day. Hope this make sense
I am wondering how this can be queried. I started the query:
DECLARE @id_check INT
DECLARE @batchSize INT
DECLARE @results INT
SET @results = 1 --stores the row count after each successful batch
SET @batchSize = 100 --How many rows you want to operate on each batch
SET @id_check = 0 --current batch
-- when 0 rows returned, exit the loop
WHILE (@results > 0)
BEGIN
SELECT * -- This is just an example to generalize result for now
FROM empdata
SET @results = @@ROWCOUNT
-- next batch
SET @id_check = @id_check + @batchSize
END
The result I am aiming for is to return batch 1 to return 100 values, then batch 2 to return the next 100 and so on
Any help would be appreciated!