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 |
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2008-11-04 : 16:56:00
|
| Hi guys. I am really new to SQL.Here is my question.I have a function with 2 parameters which could do a calculation of the two numbers. Now I want to use that function to calculate the value for (1,2),(2,3),(3,4).....(399,400) and make the result as a table.I do not know whether SQL has any "FOR" clause to do that repeat. anyone could help me?Thanks a lot |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-11-04 : 17:13:12
|
| This will generate the pairs that you want. You can also create a function that will return a table based on the pairs. The more specific you are in your request, the more we can help you get the answer you need.DECLARE @x intDECLARE @table TABLE (x int,y int)SET @x = 1WHILE @x < 400BEGIN INSERT INTO @table SELECT @x,@x+1 SET @x = @x+1 ENDSELECT * FROM @tableJim |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2008-11-04 : 17:13:53
|
You could use a WHILE loop.Or Jeff probably has a fancy CROSS JOIN solution he'll throw out.EDIT: D'oh! ---------------------------EmeraldCityDomains.com |
 |
|
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2008-11-04 : 18:39:45
|
| Thank you guys.I did it use the WHILE loop.Thanks a lot |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-05 : 01:47:05
|
| orDECLARE @x intDECLARE @table TABLE (x int,y int)INSERT INTO @tableSELECT number,number+1 FROM master..spt_valuesWHERE type='p' and number between 1 and 399SELECT * FROM @tableMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-05 : 02:08:10
|
if sql 2005. you can use CTE alsoDECLARE @EndVal intSET @EndVal=400;With CTE (num1,num2)AS(SELECT 1,2 UNION ALL SELECT num1+1,num2+1 FROM CTE WHERE num2+1<=400)SELECT * FROM CTEOPTION (MAXRECURSION 1000) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-05 : 02:16:48
|
Alsoselect number as num1, number+1 as num2 from( select row_number() over(order by name) as number from syscolumns) as twhere number<400 MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|