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