| Author |
Topic |
|
jeff06
Posting Yak Master
166 Posts |
Posted - 2007-06-13 : 10:36:58
|
| I have a table t1id maxday1 42 63 34 105 10000I want to expand it to t2id d maxday1 0 41 1 41 2 41 3 41 4 42 0 62 1 62 2 62 3 62 4 62 5 62 6 63 0 33 1 33 2 33 3 34 0 10------How can I do that? Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-13 : 10:45:16
|
are you using SQL Server 2000 or 2005 ? KH |
 |
|
|
jeff06
Posting Yak Master
166 Posts |
Posted - 2007-06-13 : 10:49:59
|
| version 8.00should be 2000 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-13 : 10:53:02
|
[code]declare @table table( id int, maxday int)insert into @tableselect 1, 4 union allselect 2, 6 union allselect 3, 3 union allselect 4, 10 union allselect 5, 10000declare @max intselect @max = max(maxday) from @tableselect t.id, n.NUMBER as d, t.maxdayfrom @table t inner join F_TABLE_NUMBER_RANGE(0, @max) n on n.NUMBER <= t.maxday order by t.id, d-- for SQL Servr 2005 onlyselect t.id, n.NUMBER as d, t.maxdayfrom @table t cross apply F_TABLE_NUMBER_RANGE(0, t.maxday) norder by t.id, d[/code]F_TABLE_NUMBER_RANGE is from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685 KH |
 |
|
|
jeff06
Posting Yak Master
166 Posts |
Posted - 2007-06-13 : 10:58:34
|
| Thanks you for your replyI got following error msg after run the code.Server: Msg 208, Level 16, State 1, Line 5Invalid object name 'F_TABLE_NUMBER_RANGE'.how can I fix this problem?Thanks again |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
jeff06
Posting Yak Master
166 Posts |
Posted - 2007-06-13 : 11:17:45
|
| I tried to use this codeCREATE FUNCTION xf_TableNumberRange(@nMin INT, @nMax INT)RETURNS @t TABLE (number INT) AS BEGIN DECLARE @i INTSET @i = @nMinWHILE @i <= @nMaxBEGININSERT INTO @t (number) VALUES (@i)SELECT @i = @i + 1ENDRETURNENDThe problem is that I have no rigth to create function can You give me some more suggestions? Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-13 : 11:36:44
|
| Create a table with numbers from 1 to 100 and do join with it instead of the functionMadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-13 : 11:36:58
|
can you create a table ? then create a tally table KH |
 |
|
|
jeff06
Posting Yak Master
166 Posts |
Posted - 2007-06-13 : 12:32:52
|
| THX for all suggestions! |
 |
|
|
|