- Este tópico contém 2 respostas, 2 vozes e foi atualizado pela última vez 18 anos atrás por chduarte.
-
AutorPosts
-
1 de fevereiro de 2007 às 9:47 pm #78588wesleyNetParticipante
Olá galera
Estou tentando usar cursor dentro de uma função. A função retorna
o último id mais um de uma determionada tabela.Segue:
Create or replace Function ULT_IDLAN_COF_PAGREC( string_campo in varchar2 ) Return NUMBER is
v_result NUMBER(5);
–Função retorna o último IDLAN conforme tipo de pagamento, campo FLAN.PAGREC
cursor C1 is select (Max(IDLAN)+1) util_id from RM.FLAN WHERE PAGREC=’2′;
Begin
OPEN C1;
fetch C1 INTO UTIL_ID;
if string_campo = ‘2’ then
v_result := UTIL_ID;
End if;
Return (v_result);
CLOSE C1;
end;[b]
Estou tentando atribuir o UTIL_ID para a variável mas não dá certo:
SQL> show errors;
Errors for FUNCTION ULT_IDLAN_COF_PAGREC:LINE/COL ERROR
——– ————————————————————-
7/2 PL/SQL: SQL Statement ignored
7/16 PLS-00201: identifier ‘UTIL_ID’ must be declared
9/1 PL/SQL: Statement ignored
9/13 PLS-00201: identifier ‘UTIL_ID’ must be declaredObrigado pela Atenção. 😀 [/b]
1 de fevereiro de 2007 às 10:21 pm #78589wesleyNetParticipanteResolvido:
Create or replace Function IDLAN_PAGREC( string_campo in varchar2 ) Return NUMBER is
–Retorna o último IDLAN conforme tipo de pagamento, campo FLAN.PAGREC
v_result NUMBER(5);
ID RM.FLAN.IDLAN%Type;
cursor C1 is select max(IDLAN) from RM.FLAN WHERE PAGREC=’2′;
Begin
OPEN C1;
fetch C1 INTO ID;
if string_campo = ‘1’ then
v_result := ID+1;
End if;
if string_campo = ‘2’ then
v_result := ID+1;
End if;
Return (v_result);
CLOSE C1;
end;[quote=”wesleyNet”:1o1pvdfo]Olá galera
Estou tentando usar cursor dentro de uma função. A função retorna
o último id mais um de uma determionada tabela.Segue:
Create or replace Function ULT_IDLAN_COF_PAGREC( string_campo in varchar2 ) Return NUMBER is
v_result NUMBER(5);
–Função retorna o último IDLAN conforme tipo de pagamento, campo FLAN.PAGREC
cursor C1 is select (Max(IDLAN)+1) util_id from RM.FLAN WHERE PAGREC=’2′;
Begin
OPEN C1;
fetch C1 INTO UTIL_ID;
if string_campo = ‘2’ then
v_result := UTIL_ID;
End if;
Return (v_result);
CLOSE C1;
end;[b]
Estou tentando atribuir o UTIL_ID para a variável mas não dá certo:
SQL> show errors;
Errors for FUNCTION ULT_IDLAN_COF_PAGREC:LINE/COL ERROR
7/2 PL/SQL: SQL Statement ignored
7/16 PLS-00201: identifier ‘UTIL_ID’ must be declared
9/1 PL/SQL: Statement ignored
9/13 PLS-00201: identifier ‘UTIL_ID’ must be declaredObrigado pela Atenção. 😀 [/b][/quote]
1 de fevereiro de 2007 às 11:19 pm #78591chduarteParticipanteO melhor é voce reescrever esta funcao assim:
Create or replace Function IDLAN_PAGREC( string_campo in varchar2 ) Return NUMBER is
–Retorna o último IDLAN conforme tipo de pagamento, campo FLAN.PAGREC
v_result NUMBER(5);
ID RM.FLAN.IDLAN%Type;
Begin
select max(IDLAN) into ID from RM.FLAN WHERE PAGREC=’2′;
if (string_campo = ‘1’ || string_campo = ‘2’) then
v_result := ID+1;
End if;
Return (v_result);
end;Note que na sua funcao o close do cursor esta depois do return. Vc pode gerar problemas de cursores no seu banco.
No exemplo acima, a query deve retornar somente 1 linha.
[]
-
AutorPosts
- Você deve fazer login para responder a este tópico.