Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to turn single rows into multiples.... 1

Status
Not open for further replies.

Peager

Programmer
May 27, 2003
140
US
I need a good set based solution to run a select statement that generates :

131234 2
134321 5
154234 3

I need:
131234
131234
134321
134321
134321
134321
134321
154234
154234
154234

I can think of several convoluted ways of getting here but there has to be a slick set-based solution.

Anybody?

Much thanks in advance.

Paul


 
Try
Code:
declare @t table (iNumber int, Occurrences int)
insert into @t 
select
131234,    2
union all
select
134321,    5
union all select
     154234,    3
     
select iNumber, Occurrences, number
from @t T inner join numbers on number between 1 and  Occurrences

assuming that you have Numbers table in the database. If you don't, check a script of creating this table in this blog
Passing ranges to SP

PluralSight Learning Library
 
Markos,

What is 'numbers' (on the inner join in the last line?


Msg 208, Level 16, State 1, Line 11
Invalid object name 'numbers'.
 
Markos,

Never mind, I got it.... It's a table of integers.....

Works GREAT!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top