I have to do one query to get one parameter (I need a "1"), but this parameter can be in different cells, for example:
+--------------+--------------+--------------+
|....cell1.....|.....cell2....|....cell3.....|
+--------------+--------------+--------------+
|....here......|.....here.....|....or here...|
+--------------+--------------+--------------+
I have to capture and after, if its equals to one do other query to get what I want. I try to do a procedure with this code:
SET TERM !! ;
CREATE PROCEDURE GET_CASH(
TICKET VARCHAR(15))
RETURNS ( TOTAL integer
)
AS
BEGIN
FOR SELECT a.ID_FPAY1, a.FPAY1_IMPORT, a.ID_FPAY2, a.FPAY2_IMPORT, a.ID_FPAY3, a.FPAY3_IMPORT
FROM TB_TICKETS_HIS a WHERE a.ID_TICKET = TICKET
DO
BEGIN
IF a.ID_FPAY1 = 1 THEN TOTAL = a.FPAY1_IMPORT;
IF a.ID_FPAY2 = 1 THEN TOTAL = a.FPAY2_IMPORT;
IF a.ID_FPAY3 = 1 THEN TOTAL = a.FPAY3_IMPORT;
SUSPEND;
END
END!!
SET TERM ; !!
When execute the query apears this error:
SQL Message : -104
Invalid token
Engine Code : 335544569
Engine Message :
Dynamic SQL Error
SQL error code = -104
Token unknown - line 11, column 5
DO
I try to it works deleting the FOR and Do, appears this error:
SQL Message : -104
Invalid token
Engine Code : 335544569
Engine Message :
Dynamic SQL Error
SQL error code = -104
Token unknown - line 11, column 5
BEGIN
Someone can help me with this procedure or explain me what is failing?
If you want to use a
FOR SELECT .. DO
(or a singleton select) in Firebird stored procedures, then you need to useINTO
to populate parameter values.See also the Firebird reference on
FOR SELECT
:In other words, depending on what this stored procedure is meant to do, you need to declare additional return variables or local variables to hold the relevant fields of the select, for example:
However you can simplify this a lot by doing that logic within the select statement:
The reverse order is only to preserve the same logic as you had in your original snippet. Technically, to preserve that logic, it would need extra handling to preserve the previous value of
total
as the code you wrote would do, but I assume that is a bug in your code.