# Stored Procedure to return values from x to y with increment z

This is simple (I think), just have not done anything like it yet

If a table has values x, y, and z

(where x is "low value", y is High value" and z is "increment")

in a row I am calling with variable "a", how do I return a set of rows that start with x, end with y, and move upwards with the increment z?

for example, x=2, y=10, z=2

the stored procedure returns

1- 2
2- 4
3- 6
4- 8
5- 10

thanks! It is urgent I learn this asap to forward my project.
###### 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.

Commented:
Could you post the table structure, or at least the columns of interest, and indicate which column(s) contain the low, high, and increment?

I'm not getting the question.
Commented:
Not getting the question either. This isn't what you meant or is it?

declare @x int, @y int, @z int

set @x = 2
set @y = 10
set @z = 2

declare @current int
set @current = @x

declare @tmp table(id int identity(1,1) not null, [index] int)

while @current<=@y
begin
insert into @tmp values(@current)
set @current = @current + @z
end

select * from @tmp
Author Commented:
CJ_S, it looks like you are getting the idea, but the sp you wrote didn't return any records, which I need.

Let me see if I can be more clear:

There is a table that contains "start" (x), "end" (y) and "increment" (z) values. A variable (a) calls the correct row.

upon selecting that row, new rows need to be returned that start with x and increase by increment z until they end up at y.

does that make more sense? Sorry I am not familiar enough with sql terminology to explain better.

Commented:
Works fine for me:

declare @a int

-- inner parameters
declare @x int, @y int, @z int

select @x = start, @y = end, @z = increment from YOURTABLE

declare @current int
set @current = @x

declare @tmp table(id int identity(1,1) not null, [index] int)

while @current<=@y
begin
insert into @tmp values(@current)
set @current = @current + @z
end

select * from @tmp
Commented:
Oh, one more change:
select @x = start, @y = end, @z = increment from YOURTABLE
should be:
select @x = start, @y = end, @z = increment from YOURTABLE where id=@a
Author Commented:
It all looks good but I keep getting "the stored procedure executed correctly but did not return any records"
Commented:
Here's a full sample which also creates a RES table with the values. This is a full example

drop table RES

create table RES
(
id int identity(1,1) not null constraint RES_PK_id PRIMARY KEY,
start int not null,
[end] int not null,
increment int not null
)

insert into RES values(4, 10, 2)

declare @a int
declare @x int, @y int, @z int

set @a = 1

select @x = start, @y = [end], @z = increment from RES

declare @current int
set @current = @x

declare @tmp table(id int identity(1,1) not null, [index] int)

while @current<=@y
begin
insert into @tmp values(@current)
set @current = @current + @z
end

select * from @tmp
Commented:
oh, and it does work for me so if this does not return any results for you, you might want to check out the actual values in your table.

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.

Author Commented:
how strange, I copied the full example into a stored procedure and still got   "the stored procedure executed correctly but did not return any records"

I am using sql server 2000 controlled through an ACCESS 2002 project and here is all the text from the SP:

ALTER PROCEDURE StoredProcedure19

AS
drop table RES

create table RES
(
id int identity(1,1) not null constraint RES_PK_id PRIMARY KEY,
start int not null,
[end] int not null,
increment int not null
)

insert into RES values(4, 10, 2)

declare @a int
declare @x int, @y int, @z int

set @a = 1

select @x = start, @y = [end], @z = increment from RES

declare @current int
set @current = @x

declare @tmp table(id int identity(1,1) not null, [index] int)

while @current<=@y
begin
insert into @tmp values(@current)
set @current = @current + @z
end

select * from @tmp

Author Commented:
OK, it works when pulled into the datagrid, I just couldn't see it directly in the stored procedure- thanks!
Commented: