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.
Author |
Topic |
hosir
Starting Member
1 Post |
Posted - 2010-07-03 : 06:59:33
|
I have the following table. seq quantity1 12 33 4I want to insert multiple rows into temp table such that the temp table containsseq 12223333Do you know how to use one sql statement, instead of cursor looping to do that?Thanks. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-03 : 09:04:48
|
[code]declare @sample table( seq int, quantity int)insert into @sampleselect 1, 1 union allselect 2, 3 union allselect 3, 4-- Using master..spt_values / number tableselect s.seqfrom @sample s inner join master..spt_values v on v.type = 'P' and v.number >= 1 and v.number <= s.quantityorder by s.seq-- Using number table functionselect s.seqfrom @sample s cross apply dbo.F_TABLE_NUMBER_RANGE(1, s.quantity)order by s.seq-- using recursive cte; with rcteas( select seq, quantity from @sample union all select seq, quantity = quantity - 1 from rcte where quantity > 1)select seqfrom rcteorder by seq[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|