all groups > sql server programming > february 2004 >
You're in the

sql server programming

group:

Select next value from a group



Select next value from a group Bent Lund
2/29/2004 10:29:05 PM
sql server programming: Hello, I have a problem getting the value I want here ;-)

I have a view looking like this:

ITEM_SEQ_NO int unique ID for item
CAST_ID varchar(20) Batch ID for n ITEM's
STEP_NO int step in production 1-20
STEP_ORDER_NO int order of item within step



Data could look like this

Item Cast STEP Order
1 AA 1 1
2 AA 1 2
3 AA 2 1
4 BB 2 2
5 BB 3 1

I then need to have a SP or something that I can use to fint the NEXT
CastID giving a CastId as parameter.

The sort-order will allways be STEP_NO, STEP_ORDER_NO asc.

So if I want to know the Next cast giving AA as input the function will
return BB.


The solution is possibly simple, I just don't see it...
Any ideas?



Thanks in advance,

regards Bent

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Bent Lund
3/1/2004 1:29:11 AM
Hi, yes I see your way of thinking here - but I believe this has to be
done by using a crusor.

thanks, regards

Bent

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Jacco Schalkwijk
3/1/2004 7:22:50 AM
CREATE PROCEDURE next_cast_ID
@cast _ID VARCHAR(20)
AS
SET NOCOUNT ON

SELECT TOP 1 Cast_ID
FROM cast_view
WHERE step_no > (SELECT MAX(step_no FROM cast_view WHERE cast_ID = @cast_ID)
OR(
step_no = (SELECT MAX(step_no FROM cast_view WHERE cast_ID = @cast_ID)
AND step_Order_no > (SELECT MAX(step_order_no FROM cast_view WHERE
cast_ID = @cast_ID)
)
ORDER BY step_no DESC, step_order_no DESC


--
Jacco Schalkwijk
SQL Server MVP



[quoted text, click to view]

Re: Select next value from a group Jacco Schalkwijk
3/1/2004 9:44:56 AM
I see no reason why you would need a cursor. What requirement do you have
that my solution doesn't satisfy?

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

Re: Select next value from a group Roji. P. Thomas
3/1/2004 3:05:50 PM
[quoted text, click to view]
Why you thin using cursor is a better solution?



--
Roji. P. Thomas
SQL Server Programmer
[quoted text, click to view]

Re: Select next value from a group Bent Lund
3/2/2004 4:59:17 AM
Thank you jacco!

The solution seems to work fine, my problem is the return value now.

I execute the procedure from VB and my question is - should i read the
@Cast_ID as the returned value, or something else?

Usually I use separate parameter for in and outbound data, but I can't
seem to fit in a parameter for outgoing data here.



Thanks for your help, regards

Bent

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Bent Lund
3/2/2004 5:29:20 AM
Hi, how do i get the output parameter here?

I get the error message "Invalid Column Name 'CAST_ID' for some reason I
don't understand.



CREATE PROCEDURE MTS_GET_NEXT_CAST

@cast_id varchar(20),
@cast_id_out varchar(20) output
AS

SELECT TOP 1 [CAST_ID] as CAST_ID
FROM V_STEP_DATA
WHERE step_no < (SELECT MAX(step_no) FROM V_STEP_DATA WHERE cast_ID =
@cast_id)
OR(
step_no = (SELECT MAX(step_no) FROM V_STEP_DATA WHERE cast_ID =
@cast_id)
AND step_Order_no > (SELECT MAX(step_order_no) FROM V_STEP_DATA
WHERE
cast_ID = @cast_id)
)
ORDER BY step_no DESC, step_order_no desc


select @cast_id_out = [CAST_ID]

GO


Regards,


Bent S. Lund
System Developer
MCP VB

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Bent Lund
3/2/2004 6:29:41 AM
Thanks,

I didn't see that one!

Thanks a lot - works perfect now as far as I can see.

I tried to use SELECT instead of SET. Possibly the result would have
been the same if I had enclosed it all in parantheses



regards,

Bent S. Lund
System Developer
MCP VB

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Joe Celko
3/2/2004 8:53:08 AM
Anyone who talks about sequential numbers in SQL is probably about to
use cursors to mimick a file system -- its one of the warnign signs.

[quoted text, click to view]
will return BB. <<

Build a small look up table of pairs of letters.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Re: Select next value from a group Jacco Schalkwijk
3/2/2004 2:05:05 PM
Hi Bent,

You ahve to assign the value that is returned by the select statement to the
variable:

CREATE PROCEDURE MTS_GET_NEXT_CAST

@cast_id varchar(20),
@cast_id_out varchar(20) output
AS

SET @cast_id_out = (SELECT TOP 1 [CAST_ID] as CAST_ID
FROM V_STEP_DATA
WHERE step_no < (SELECT MAX(step_no) FROM V_STEP_DATA WHERE cast_ID =
@cast_id)
OR(
step_no = (SELECT MAX(step_no) FROM V_STEP_DATA WHERE cast_ID =
@cast_id)
AND step_Order_no > (SELECT MAX(step_order_no) FROM V_STEP_DATA
WHERE
cast_ID = @cast_id)
)
ORDER BY step_no DESC, step_order_no desc)




GO


--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

AddThis Social Bookmark Button