# Adding a given start number and end number in T-SQL without using loop

Hi, I want to create a stored procedure where you pass a start number and an end number. For example 1 as your start number and 1000 as your end number, then the output should be the sum of 1-1000.

We can do it easily using loops. But my requirement is to do it without using loops.

Can any one please give me that logic ?

Many Thanks
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Microsoft SQL Server Developer, Architect, and AuthorCommented:
>But my requirement is to do it without using loops.
Why's that?  Homework?
0
freelancerCommented:
sum of 1-1000 = 999

I think you mean 1 + 2 + 3 + 4 ... + 1000
(the sum of all numbers from 1 to 1000)

is that correct?
0
Author Commented:
Hi Portlet , yes that is correct.
0
Author Commented:
I think you mean 1 + 2 + 3 + 4 ... + 1000
(the sum of all numbers from 1 to 1000)
0
freelancerCommented:
and Jim's questions?
0
Author Commented:
yes kind of Homework :-)
0
freelancerCommented:
thanks for the honesty.

we can only guide you or help if your current approach has hit a snag

what have you tried so far?
0
freelancerCommented:
tip:
does the name Karl Friedrich Gauss
mean anything to you?
0
Author Commented:
yes he is a German mathematician, why ?
0
Author Commented:
Hi Portlet, I am not a sql guru yet and hence I posted the question on this website...Yes I have done this but using loops...But now the requirement is without  using loops.

can you suggest me of any idea ?
0
freelancerCommented:
Gauss worked out a method of quickly summing all numbers from 1 to 100

several maths sites explain the technique

it requires working out a repeating pattern, and I believe it would apply here (with some effort)
0
Author Commented:
so can I not got any help from this site then ? I do not have enough time for research and hence I have posted it here.
0
Sr. MS SQL DBA and Technical Account ManagerCommented:
Hi there,

here's the code for you for calculating 1 to N:

SET @sum = (1 + @lastnumber) * (@lastnubmer / 2)

But we need to calculate M to N, which is basically (1 to N) - (1 to M). So it will be:

SET @sum = ((1 + @lastnumber) * (@lastnumber / 2)) - ((1 + @firstnumber) * (@firstnumber / 2))
0
Author Commented:
Hi Jim,Ok, I was not aware of this policy, but thanks for letting me know and I will make sure I will follow these guidelines from now on.
0
Author Commented:
Thanks for the help , but your solution will not work where I have 5 as the start number and 10 as the end number. However, I have worked my self the right solution which is below and works like a charm in all cases.

declare @x as int;

declare @y as int;

set @x =5;

set @y =10;

With raf

as

(

select  @x as num

union all

select num + 1

from raf

where num < @y

)

select sum(num)

from raf

option (maxrecursion 1000)
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Microsoft SQL Server Developer, Architect, and AuthorCommented:
Thanks.  In that case, give this a whirl..

``````Declare @firstnumber int = 0, @lastnumber int = 6
Declare @runningsum int = 0, @counter int = @firstnumber

WHILE @counter <= @lastnumber
begin
SET @runningsum = @runningsum + @counter
SET @counter = @counter + 1
end

SELECT @runningsum
``````
0
Author Commented:
Hi Jim,

Thanks for this, but As you can see from my above posts yesterday, I have already done this using loops but the requirement was to achieve this without using loops...

Cheers
0
Microsoft SQL Server Developer, Architect, and AuthorCommented:
Again, spell out the requirement.  Ten bucks says you didn't get it from a business user, so either this is coming from a homework assigment (which means it's against policy for us to answer it) or a DBA got a wild hair and is torturing you for fun.

Spill.
0
Author Commented:
Hi Jim,

Yes you are right, this is not exactly homework...our DBA has challenged me to do this without using loops .
However he was happy with my solution which I have described above and it works in all scenarios. I did not use a loop , however I have just used recursive CTE :-)
0
Author Commented:
good
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.