469,621 Members | 1,740 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

sql statement

I am having a database table which has a column of datatype number.
i am having a function which takes a number as the parameter and
returns a number which is the next
highest number in the database than the passed parameter.

suppose if i my database is having the data in a column as below

1
2
3
4
5

If a pass 3 to my function, i should get 4 as the return value.
If a pass 4 to my function, i should get 5 as the return value.

Can anybody write a sql statement to do this operation. I am using sql
server, but i want the query to get executed on any database.
Jul 20 '05 #1
1 2254
"harish" <ha*******@yahoo.com> wrote in message
news:ff**************************@posting.google.c om...
I am having a database table which has a column of datatype number.
i am having a function which takes a number as the parameter and
returns a number which is the next
highest number in the database than the passed parameter.

suppose if i my database is having the data in a column as below

1
2
3
4
5

If a pass 3 to my function, i should get 4 as the return value.
If a pass 4 to my function, i should get 5 as the return value.

Can anybody write a sql statement to do this operation. I am using sql
server, but i want the query to get executed on any database.


CREATE TABLE Numbers
(
n INT NOT NULL PRIMARY KEY
)

INSERT INTO Numbers (n)
VALUES (1)
INSERT INTO Numbers (n)
VALUES (2)
INSERT INTO Numbers (n)
VALUES (3)
INSERT INTO Numbers (n)
VALUES (4)
INSERT INTO Numbers (n)
VALUES (5)

CREATE FUNCTION NextNumber (@n INT)
RETURNS INT
AS
BEGIN
RETURN(
SELECT MIN(n)
FROM Numbers
WHERE n > @n
)
END

SELECT dbo.NextNumber(3)

4

Regards,
jag
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Fábio Mendes | last post: by
13 posts views Thread by eman1000 | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
28 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by dspfun | last post: by
23 posts views Thread by florian.loitsch | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.