PriceCheaperton
PriceCheaperton

Reputation: 5349

How to execute a stored procedure multiple times

I have a stored procedure which i execute like this:

exec sp_storedProc '123','ME', '333',NULL

I have 400 different values I need to specify. How do I execute my stored procedure with different values at once?

Upvotes: 11

Views: 25448

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

You may try to do it using CURSORS

DECLARE @param INT

DECLARE curs CURSOR LOCAL FAST_FORWARD FOR
    SELECT parameter FROM table_name WHERE ...

OPEN curs

FETCH NEXT FROM curs INTO @param

WHILE @@FETCH_STATUS = 0 BEGIN
    EXEC sp_storeProc  @param
    FETCH NEXT FROM curs INTO @param
END

CLOSE curs
DEALLOCATE curs

Upvotes: 23

Related Questions