I want to execute the stored procedure by passing parameter values from a table for a batch size of 100. Once the stored procedure call is made for first batch of 100, move on to next batch of 100 and repeat till all the rows in the table are done. The table has a column which will get updated making sure that same row doesn't get picked up when i do
select top (batch_size) *
from table
I question is more about how to implement batching for the stored procedure call.
EDIT: Added OP's comment:
Example, table1 below has 1000 rows. my first batch is for 500 rows. Making one stored procedure call for each row.
CREATE TABLE table1
(
id INT IDENTITY,
col1 VARCHAR(100),
status VARCHAR(50)
)
DECLARE @batch_size INT
SET @batch_size = 5
INSERT INTO table2
SELECT TOP(500) col1
FROM table1
WHERE status IS NULL
ORDER BY id
SELECT TOP(1)
@id = id, @col1 = col1, @col2 = col2
FROM table2
ORDER BY id
WHILE @@rowcount > 0
BEGIN
EXEC SP param1 = col1, param2 = col2
UPDATE table1
SET status = 'closed'
SELECT TOP(1) @id = id, @col1 = col1, @col2 = col2
FROM table2
WHERE id > @id
ORDER BY id
END
I'm not saying you're going about this the best way, but to answer your question, you can simply put your existing code inside a WHILE loop that checks
WHILE EXISTS(SELECT * FROM Table1 WHERE Status IS NULL)
Your code will keep getting batches of 500 rows at a time until there are no more rows in Table1 with a NULL for Status.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.