Skip to content
Advertisement

Dynamic Generate rows without using loops

How to dynamic generate row from max value for example if i am passing max value 7 Store procedure should return value 1 2 3 4 5 6 7

without using loops

Advertisement

Answer

create PROCEDURE [dbo].GenerateSequence 
    @MaxLimit int
    
AS
BEGIN

;with numcte
AS  
(  
  SELECT 1  [Sequence] 
  UNION all  
  SELECT  [Sequence] + 1 FROM numcte WHERE [Sequence] < @MaxLimit
)
select * from numcte

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