Looping over some selected values in a stored procedure
        Posted  
        
            by macca1
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by macca1
        
        
        
        Published on 2010-06-03T21:24:45Z
        Indexed on 
            2010/06/03
            21:54 UTC
        
        
        Read the original article
        Hit count: 254
        
I'm trying to modify a stored procedure hooked into an ORM tool. I want to add a few more rows based on a loop of some distinct values in a column. Here's the current SP:
SELECT
    GRP = STAT_CD,
    CODE = REASN_CD
FROM dbo.STATUS_TABLE WITH (NOLOCK)
Order by STAT_CD, SRT_ORDR
For each distinct STAT_CD, I'd also like to insert a REASN_CD of "--" here in the SP. However I'd like to do it before the order by so I can give them negative sort orders so they come in at the top of the list.
I'm getting tripped up on how to implement this. Does anyone know how to do this for each unique STAT_CD?
© Stack Overflow or respective owner