Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-15
11
Medium Priority
?
1,612 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:JCMcNeil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 6

Expert Comment

by:robertjbarker
ID: 9757708
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.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 9758154
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
0
 

Author Comment

by:JCMcNeil
ID: 9758185
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.

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 22

Expert Comment

by:CJ_S
ID: 9758192
Works fine for me:

-- your parameter:
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
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 9758196
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
0
 

Author Comment

by:JCMcNeil
ID: 9758317
It all looks good but I keep getting "the stored procedure executed correctly but did not return any records"
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 9758332
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
0
 
LVL 22

Accepted Solution

by:
CJ_S earned 2000 total points
ID: 9758336
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.
0
 

Author Comment

by:JCMcNeil
ID: 9758380
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





0
 

Author Comment

by:JCMcNeil
ID: 9758449
OK, it works when pulled into the datagrid, I just couldn't see it directly in the stored procedure- thanks!
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 9758471
Glad that it's working!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question