Solved

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

Posted on 2003-11-15
11
1,598 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
  • 6
  • 4
11 Comments
 
LVL 6

Expert Comment

by:robertjbarker
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:JCMcNeil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad that it's working!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now