?
Solved

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

Posted on 2003-11-15
11
Medium Priority
?
1,609 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

800 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