Get row number from temp table with loop

Beantwortet Get row number from temp table with loop

  • Thursday, January 24, 2013 2:25 AM
     
     

    I have a variable that has comma separated values. There can be in number of values in there. I need to loop through the temp table and pull a value from each record. I get a count from the table So for example let's say there are 5 values.  Now I need to get the last value which would be the 5th record. Than while @recordcount > 0 then the 4th records value and so on. Here is what I have so far.

    DECLARE @dealercount INT
    SET @dealercount = (Select COUNT(val) from dbo.udfIntegerArrayListTable(@dealerid))
    DECLARE @dealerids int  
    WHILE    @dealercount > 0
    BEGIN
        SELECT val FROM  
         (  
         SELECT Row_Number() OVER (ORDER BY val) as rowid,
         
          val from dbo.udfIntegerArrayListTable(@dealerid)
         ) as a  
        where rowid = @dealercount

     

    Alan

All Replies