Sunday, April 15, 2007

Transact SQL - Executing Statements multiple times

In order to execute a set of SQL statements / stored procedure multiple times - I have been using the traditional method of WHILE loop. The other day Vijay suggested an easy approach making use of the GO command.

We can specify a number 'N' after GO command and the batch will be executed 'N' number of times. For example - if we want to call the stored procedure 'StoredProcedureName' 100 times - the command would be

EXEC 'StoredProcedureName'
GO 100