Skip to content
Advertisement

Iterate Through Each Record

I have an ASP.NET page which repeatedly calls a SQL stored procedure that pulls a random userID from the table Hearings_Users. What I need now is to pull each UserID in successive order. I would go down the list of UserIDs in the table until the VB function stopped calling it. I’m not sure how to do this in SQL. Thanks

Advertisement

Answer

I’d create a new stored procedure that takes a parameter of the last userID, and returns the next in sequence

The body would be like p_Hearings_ActiveUsers except it has a condition that the ID is greater than the last used

When you call this from code, you’ll need to add a parameter to the command

but it’s otherwise just like the call you have already

This approach insures the state is stored in your application, not server side, so if 2 instances are iterating through they don’t cross each other up

Efficiency tip: You don’t need to redefine the parameter each loop iteration, you can define it above the loop and inside the loop just update it’s value.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement