Subject Stored procedure problem
Author Andrea Raimondi
Hello.

What I want to do:

I have an integer value, called Token, that I have to check into my DB.
If there's an entry already about it, I have to update OccurNum field.
If it doesn't exist, I have to insert it and assign it an OccurNum of 1.
That's the first part. Now that I have in a way or another a token id,
I have to associate it with the correct file name. Thus I have to
check if the file name already exists. This being the case, I can
quit the procedure, otherwise I have to insert the file name into the
appropriate table. Not ended yet, now I have to associate the correct
token(seen above) for the correct file name.

To make things simpler for me, I have created a generator, called
IDGen, which allows me to use unique IDS in the whole database.

Small recap:

I have these tables:

CREATE TABLE FILENAMES (
ID_NAME INTEGER NOT NULL,
FILENAME VARCHAR(255) NOT NULL
);

CREATE TABLE TOKENS (
ID_TOKEN INTEGER NOT NULL,
TOKEN INTEGER NOT NULL,
OCCURNUM INTEGER NOT NULL
);

CREATE TABLE TOKFILES (
ID_TOKEN INTEGER NOT NULL,
ID_NAME INTEGER NOT NULL
);

Given the whole above, my stored proc(still lacking the last table
inserts) looks like this:

begin
/* Procedure Text */
TOKENID = 0;
Select tokens.id_token from TOKENS where TOKENS.token = :token into
:TOKENID;
if (TokenId <> 0) then
begin
select OCCURNUM from tokens where tokens.id_token = TOKENID into
:OCCNUM;
update tokens set tokens.occurnum = OccNum+1 where tokens.id_token
= TokenID;
end
else begin
select gen_id(IDGen,0) into :TOKENID;
insert into tokens values(:TOKENID,:token,1)
end
select ID_NAME from filenames into :FILEID
if ( FileID = 0 ) then
begin
Select gen_id(IDGen,0) into :FILEID
Insert into filenames values( :FILEID,:filename )
end

/* suspend;*/
end

TOKENID,FILEID and OCCNUM are variables, while TOKEN and FILENAME are
input parameters.

IBExpert complains on the line

select gen_id(IDGen,0) into :TOKENID;

which I assume being stupidly wrong but can't figure out how to call the
generator correctly inside a stored procedure.

Plus, I'm not sure my approach being generally correct in this stuff.
Any pointers/ideas/help?

TIA,

Andrew