Solved

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

Posted on 2003-11-15
11
1,607 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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