I am a beginner in PL/SQL. I need to write a function with following details:
Create a function named 'find_transaction_type' that will accept the transaction_type_id as input. Based on this input, the function must return the transaction type name of type varchar.
Function name : find_transaction_type,
Input Parameter : transaction_type_id in int
Design rules:
1)If transaction type id(i.e, transaction_type_id) passed as input, matches with the id in the transaction table,then it returns the type of the given transaction_type_id.
2)If the transaction type id passed as input, does not match with the id in the transaction table,then it throws ' no_data_found' exception and displays it with the text as ' No such Type'
Note: Use variable to print the exceptions instead of 'dbms_output.put_line' ie: umpire_name := 'No such umpire';
My Solution is:
create or replace function find_transaction_type(transaction_type_id in integer) return varchar is
transaction_type_name varchar(255);
type_id integer;
error_msg varchar(255);
begin
error_msg := 'No such Type';
select id
into type_id
from transaction_type;
if type_id = transaction_type_id
then
select type
into transaction_type_name
from transaction_type
where id = transaction_type_id;
return(transaction_type_name);
else
raise no_data_found;
end if;
exception
when no_data_found then
raise_application_error(-10403, error_msg);
end;
/
What's wrong with my code?
selecthas no where clause so will return all rows fromtransaction_type, not just the one you want.