SET TERM ^ ;

CREATE OR ALTER function CONTROLEUSUARIO (
    PERMISSAO integer)
returns varchar(500)
as
declare variable RET varchar(500);
declare variable VALOR_EXT varchar(500);
BEGIN       
    RET = '0';

    VALOR_EXT = (
        SELECT FIRST 1
               VALOR_EXT
          FROM TBCONTROLE_USUARIO
         WHERE USUARIO_ID = (SELECT FIRST 1 U.CODIGO FROM TBUSUARIO U WHERE U.USUARIO = CURRENT_USER)
           AND ID         = :PERMISSAO
    );

    IF ( VALOR_EXT IS NOT NULL ) THEN
    BEGIN
        RET = VALOR_EXT;
    END

    RETURN RET;
END
^

SET TERM ; ^

CREATE INDEX ITBDEFEITO_TRANSACAO_ITEM_20
ON TBDEFEITO_TRANSACAO_ITEM (ORIGEM);

