estoy queriendo hacer que una función me valide si un dato que le pase existe en mi tabla o no, eh intentado con EXISTS y con COUNT() pero todavia no le agarro bien la mano para que funcione mi consulta:
create or replace function fn_Validar(job_recibido employees.JOB_ID%TYPE)
return employees.JOB_ID%TYPE
IS
validar employees.JOB_ID%TYPE;
BEGIN
if ((COUNT(*) from hr.employees WHERE JOB_ID = job_recibido) > 1)
then
validar := 'TRUE';
ELSE
validar := 'FALSE';
END IF;
RETURN validar;
END;
Ésta es una de las formas que traté de resolver éste ejercicio pero solamente consigo que me tire error:
Errors: FUNCTION FN_VALIDA Line/Col: 8/18 PLS-00103: Encountered the symbol "FROM" when expecting one of the following: ) , * & = - + < / > at in is mod remainder not rem => <an exponent (.)> <> or != or ~= >= <= <> and or like like2 like4 likec as between || multiset member submultiset Line/Col: 8/66 PLS-00103: Encountered the symbol ")" when expecting one of the following: . ( * @ % & - + ; / at mod remainder rem return returning <an exponent (.)> and or || multiset
Alguna ayuda o recomendación que me puedan dar para poder resolverlo?
Muchas gracias
EDIT:
usando SELECT antes del COUNT() me tira el siguiente error:
Error: Line/Col: 9/14 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:( - + case mod new not null continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date pipe
Otra forma de probarlo fue:
create or replace function fn_Valida_Job(job_id_recibido hr.employees.JOB_ID%TYPE)
return hr.employees.JOB_ID%TYPE
IS
validar hr.employees.JOB_ID%TYPE;
cantidad number := 0;
BEGIN
select COUNT(*) INTO cantidad
from hr.employees WHERE JOB_ID = job_id_recibido
if cantidad > 0 then
validar := 'TRUE';
ELSE
validar := 'FALSE';
END IF;
RETURN validar;
END;
pero con el siguiente error:
Errors: FUNCTION FN_VALIDA_JOB Line/Col: 9/5 PL/SQL: SQL Statement ignored Line/Col: 12/5 PL/SQL: ORA-00933: SQL command not properly ended Line/Col: 15/5 PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge json_exists json_value json_query json_object json_array
Line/Col: 20/5 PLS-00103: Encountered the symbol "RETURN"