I am trying to loop through my counter to create a dynamic sql query which should finally look like
I am trying to use this approach to get the final query but doesnt seems to work
the final ouput that i am looking for is 110 111 112 but it comes as 112 Any suggestion would be helpfull
I am trying to use this approach to get the final query but doesnt seems to work
Code:
declare @CurrentRow int set @CurrentRow =0; declare @RowsToProcess int declare @FinalHistoricalQuery varchar(5000) WHILE @CurrentRow<3 BEGIN SET @FinalHistoricalQuery =' select 11'+convert(varchar(20),@CurrentRow) + ' union ' SET @CurrentRow=@CurrentRow+1 END SET @FinalHistoricalQuery = left(@FinalHistoricalQuery,len(@FinalHistoricalQuery)-6) exec (@FinalHistoricalQuery)
Comment