Solved

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

Posted on 2003-11-15
11
1,605 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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