Stored Procedure Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ann

    Stored Procedure Question

    I need to write a stored procedure that takes the following data:

    ID Sequence ColumnName
    71 1 CustomerAccount Name
    71 2 RevenueClass

    And loops through the record set to concatenate Column Name into a string.

    The result should look like this:

    ID ColumnName
    71 CustomerAccount NameRevenueClas s

    Can you tell me how to do this?
  • David Portas

    #2
    Re: Stored Procedure Question

    SELECT id,
    MAX(CASE sequence WHEN 1 THEN columnname ELSE '' END)+
    MAX(CASE sequence WHEN 2 THEN columnname ELSE '' END)+
    MAX(CASE sequence WHEN 3 THEN columnname ELSE '' END)+
    MAX(CASE sequence WHEN 4 THEN columnname ELSE '' END)+
    ...
    FROM SomeTable
    GROUP BY id

    --
    David Portas
    SQL Server MVP
    --


    Comment

    Working...