I'm trying to better understand set-based logic and simplify a piece of code I'm working on. Here's an example of what I am currently working with (it doesn't work currently for reasons that will quickly become apparent):
SELECT
userid,
rn = ROW_NUMBER() OVER (ORDER BY username)
FROM user
WHERE username like 'test%'
WHILE rn between 1 and 100
<RUN SP USING INFORMATION>
WHILE rn between 101 and 200
<RUN SP WITH DIFFERENT INFORMATION>
For the purposes of example, assume there are 200 rows that meet the criteria in the SELECT
statement. Also assume that we can't make changes to the user
table. My question is, without using a temp table and without (hopefully) using a WHILE
loop, how else could I handle this?
Note that the SP referenced is a rather complex stored procedure that needs to use the information within that SELECT
statement. I could handle this by using a temp table and going row by row, but I'm trying to understand other approaches to this where I could handle it in batches.
You can't call a stored procedure as part of another query, so it would be impossible to do what you're asking as a set-based operation without rewriting the stored procedures.