Subject Re: [firebird-support] I'm really new to storedProc and need a little help
Author Milan Babuskov
Just to add a few things:

Lucas Franzen wrote:
>> CREATE PROCEDURE GETPAYNUM RETURNS (hightn INTEGER) AS
>> BEGIN
>> UPDATE LUTABLE SET LU_RATE=LU_RATE +1 WHERE LU_FIELD='PYNUM';
>> FOR SELECT LU_RATE FROM LUTABLE WHERE LU_FIELD='PYNUM' INTO hightn

This looks like you're be getting only one single row from this select,
so you don't need FOR SELECT ... DO ...
Just do:
SELECT LU_RATE FROM LUTABLE WHERE LU_FIELD='PYNUM' INTO :hightn

>> IF (LU_RATE IS NULL) THEN
>> BEGIN
>> INSERT INTO LUTABLE (LU_DESC, LU_FIELD, LU_RATE, LU_ORDER,
>> LU_DEF)
>> VALUES ('PayNumber', 'PYNUM', 1, 0, 'N');
>>
>> SELECT LU_RATE FROM LUTABLE WHERE LU_FIELD='PYNUM' INTO
>> hightn;

This second select is extra. You already know what value is there.

So here's my idea of it:

SET TERM !!
CREATE PROCEDURE GETPAYNUM RETURNS (hightn INTEGER) AS
BEGIN
SELECT LU_RATE FROM LUTABLE WHERE LU_FIELD='PYNUM' INTO :hightn;
IF (LU_RATE IS NULL) THEN
BEGIN
INSERT INTO LUTABLE (LU_DESC,LU_FIELD,LU_RATE,LU_ORDER,LU_DEF)
VALUES ('PayNumber', 'PYNUM', 1, 0, 'N');
LU_RATE = 1;
END
ELSE
BEGIN
UPDATE LUTABLE SET LU_RATE=LU_RATE +1 WHERE LU_FIELD='PYNUM';
LU_RATE = LU_RATE + 1;
END
SUSPEND;
END!!
SET TERM ;

--
Milan Babuskov
http://fbexport.sourceforge.net