Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select an item more then once

Author  Topic 

jacobgold
Starting Member

5 Posts

Posted - 2012-07-31 : 18:06:20
hi
i have a simple problem
i have a small table that contains 20 rows with 2 columns id and name i want to enter numbers like this lets say= 1 3 2 5 5 4 3 2 2 1 and i should get the name of id 1 and then of 3 and then of 2 and then 5 and then 5 etc. i want to get them in this order repetitive
i can't make a simple select statement like this
select name where id =1 3 2 5 5 4 3 2 2 1
because it would not be repeaated and not in this row sow what should i do?

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-07-31 : 19:21:38
what is the delimiter for 1 3 2 5 5 4 3 2 2 1? space?

declare @jacobsilver table(id int, name varchar(50))
declare @jacobatolympics table(id varchar(max))

insert into @jacobatolympics
values('1 3 2 5 5 4 3 2 2 1')

insert into @jacobsilver(id, name)
values(1, 'a'),(2,'b'),(3,'c'),(4,'c'),(5,'e'),(6,'f'),(7,'g')



select *
From @jacobsilver a
inner join (
SELECT Tags.val.value('.', 'int') AS id
FROM(
SELECT CAST('<t>' + REPLACE(id, ' ', '</t><t>') + '</t>' AS XML) AS TAG
FROM @jacobatolympics
) TAB
CROSS APPLY TAG.nodes('/t') as Tags(val)
) b

on a.id = b.id



<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-31 : 19:38:35
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;
Go to Top of Page
   

- Advertisement -