IFNULL will also handle this. It is used just like coalesce.

Sent from my iPhone

On Mar 21, 2013, at 8:49 AM, Mike Wills <mike@xxxxxxxxxxxx> wrote:

Thanks guys. I'll try that. I wasn't sure if it would return 0 if there was
no records to return.

--
Mike Wills
http://mikewills.me


On Thu, Mar 21, 2013 at 8:32 AM, Peter Dow <petercdow@xxxxxxxxx> wrote:

Hi Chuck,

I think Mike wants to return 0 if there is no record, so

SELECT COALESCE(MAX(COMMENT_KEY) + 1, 0)
FROM MPRLRCHISP
WHERE REQUEST_ID = @id

--
*Peter Dow* /
Dow Software Services, Inc.
909 793-9050
petercdow@xxxxxxxxx <mailto:petercdow@xxxxxxxxx>
pdow@xxxxxxxxxxxxxx <mailto:pdow@xxxxxxxxxxxxxx>/


On 3/20/2013 3:01 PM, CRPence wrote:
On 20 Mar 2013 13:55, Mike Wills wrote:
As I am tweaking my .NET application, I found another place where
maybe I could switch to a stored a procedure. What it needs to do is
first run

SELECT MAX(COMMENT_KEY) FROM MPRLRCHISP WHERE REQUEST_ID = @id

to get the next key for the comment. If there isn't one, I want to
return 0.

Then I run an insert using that key as a sequence number (+1).

The question being in a stored procedure, how to set that to 0 if no
result is returned on the query?
SELECT COALESCE(MAX(COMMENT_KEY), 0)
FROM MPRLRCHISP
WHERE REQUEST_ID = @id

or to also include the increment:

SELECT COALESCE(MAX(COMMENT_KEY), 0) + 1
FROM MPRLRCHISP
WHERE REQUEST_ID = @id



--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/midrange-l.


--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/midrange-l.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.