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
 General SQL Server Forums
 New to SQL Server Programming
 Rookie's question

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 int
DECLARE @table TABLE (x int,y int)

SET @x = 1

WHILE @x < 400
BEGIN
INSERT INTO @table
SELECT @x,@x+1
SET @x = @x+1

END

SELECT * FROM @table

Jim
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-05 : 01:47:05
or

DECLARE @x int
DECLARE @table TABLE (x int,y int)

INSERT INTO @table
SELECT number,number+1 FROM master..spt_values
WHERE type='p' and number between 1 and 399

SELECT * FROM @table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-05 : 02:08:10
if sql 2005. you can use CTE also

DECLARE @EndVal int
SET @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 CTE
OPTION (MAXRECURSION 1000)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-05 : 02:16:48
Also
select number as num1, number+1 as num2 from
(
select row_number() over(order by name) as number from syscolumns
) as t
where number<400


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -