Create procedure DoitNTimes @N integer = 1 As Set NoCount On While @N > 0 Begin Insert B (Col1, Col2, Col3, Col4, Col5) Select 100, 25, 'ABC', 1, A.ID From Auctions A
If you are using SQL Server 2005 or earlier, replace Set @N -= 1' with Set @N = @ N-1`
and if you really want to avoid a loop using T-SQL variables, use a CTE, not a table on disk:
Create procedure DoitNTimes @N integer = 1 As Set NoCount On With nums(num) As (Select @N Union All Select num - 1 From nums Where num > 1) Insert B (Col1, Col2, Col3, Col4, Col5) Select 100, 25, 'ABC', 1, A.ID From Auctions A Full Join nums Option(MaxRecursion 10000)
but, of course, this is also still a cycle, like any solution to this problem.
source share