Here are two or three ways to do this. My recommendation is that you use the third approach, even though it requires you to install a function.
I just want to point out that this seems like a business requirement that can perhaps be recast to make it more palatable to a database. There is something not quite right in a design that sends the input data in this manner and requires output in the way you described.
------------------------------------------------------------------------
-- CREATE TEST TABLE AND POPULATE WITH DATA.
------------------------------------------------------------------------
CREATE TABLE #tmp(id INT, NAME VARCHAR(32), seq VARCHAR(255) )
INSERT INTO #tmp (id,NAME)VALUES (1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E');
------------------------------------------------------------------------
-- QUIRKY UPDATE METHOD.
------------------------------------------------------------------------
DECLARE @y VARCHAR(255) = '';
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';
UPDATE #tmp
SET seq = @x,
@y = REPLACE(@x, CAST(id AS VARCHAR(32)), NAME),
@x = @y
FROM #tmp WITH (TABLOCKX) OPTION(MAXDOP 1)
SELECT TOP 1 seq FROM #tmp ORDER BY id DESC;
------------------------------------------------------------------------
-- CTE METHOD.
------------------------------------------------------------------------
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';
;WITH cte AS
(
SELECT id, REPLACE(@x,CAST(id AS VARCHAR(32)),NAME) AS Updated
FROM #tmp WHERE id = 1
UNION ALL
SELECT t.id,REPLACE(Updated, CAST(t.id AS VARCHAR(32)), NAME)
FROM #tmp t INNER JOIN cte c ON c.id + 1 = t.id
)
SELECT TOP 1 Updated FROM cte ORDER BY id DESC;
------------------------------------------------------------------------
-- XML PATH METHOD. You will need to install the function DelimitedSplit8K from
-- this article (in Fig 21).
-- http://www.sqlservercentral.com/articles/Tally+Table/72993/
------------------------------------------------------------------------
DECLARE @x VARCHAR(32) = '1 3 2 5 5 4 3 2 2 1';
;WITH cte AS
(
SELECT * FROM MASTER.dbo.DelimitedSplit8K(@x,' ')
)
SELECT LTRIM(RTRIM(c)) FROM
(
SELECT
' ' + NAME AS [text()]
FROM
cte c
INNER JOIN #tmp t ON t.id = c.Item
FOR XML PATH('') )T(c);
------------------------------------------------------------------------
-- CLEANUP
------------------------------------------------------------------------
DROP TABLE #tmp;