PL/SQL Function_get total balance has errors - plsql

I was trying to create a function that given a customer name, get the total balance of that customer's accounts from the account table(A#, CNAME, BAL).
When compiling in command, I got errors:
line "SELECT SUM(bal) INTO total_a_bal FROM account" ignored
and line "WHERE account.cname = v_acname" SQL cmd not properly ended.
Please help correct the errors. Any help would be greatly appreciated!
My function is :
CREATE OR REPLACE FUNCTION totbal
(v_acname IN account.cname%type)
RETURN NUMBER
IS
total_a_bal NUMBER;
BEGIN
SELECT SUM(bal) INTO total_a_bal FROM account
group by account.cname
WHERE account.cname = v_acname;
RETURN total_a_bal;
END;
/

This may not be the only problem with your function, but the WHERE clause always precedes the GROUP BY clause in a SQL query. Hence, your function should look like this:
CREATE OR REPLACE FUNCTION totbal
(v_acname IN varchar2) -- a type should follow IN, e.g. varchar2
RETURN NUMBER
IS
total_a_bal NUMBER;
BEGIN
SELECT SUM(bal) INTO total_a_bal
FROM account
WHERE account.cname = v_acname; -- WHERE always precedes GROUP BY
GROUP BY account.cname
RETURN total_a_bal;
END;

Oracle not only works with the parameter definition "account.cname%type" that is preferred method. What it does is to bind the parameter definition to the column in the database. If the column definition is changed the procedure automatically gets the changed.
This concept of defining in terms of data base columns can (and perhaps should) be extended into the variables and return types of the procedure/function itself.
This function then becomes:
CREATE OR REPLACE FUNCTION totbal
(v_acname IN account.cname%type)
RETURN account.bal%type
IS
total_a_bal account.bal%type;
BEGIN
SELECT SUM(bal) INTO total_a_bal FROM account
group by account.cname
WHERE account.cname = v_acname;
RETURN total_a_bal;
END;

Related

Procedure created with compilation errors. payment_count(l_p_count); *

Create a procedure named 'payment_count' which accepts one output parameter p_count with number as its data type. This procedure should print the count of successful payments (response_msg as 'Successful') with alias name as 'successful_payment_count'.
Hints: Procedure name : payment_count
Output parameter : p_count
Table used : payment_authorization
Function used : count
create or replace procedure payment_count(p_count output number)
as
begin
select count(response_msg='Successful') into successful_payment_count from payment_authorization;
return successful_payment_count;
end;
/
schema payment_authorization
Is this a task in an exam?
Sounds so... anyway:
You have to change keyword output to OUT, change the COUNT(response_msg='Successful') to COUNT(*) and add response_msg='Successful' to the where clause of the query.
create or replace procedure payment_count(p_count out number)
as
begin
select count(response_msg) as successful_payment_count
into p_count
from payment_authorization
where response_msg = 'Successful';
end;
/

Getting multiple values from a function

I have my CLIENTS table with the fields: (PK:ID NUMBER), PRICE NUMBER, PAYMENT_TYPE_ID NUMBER, SESSION_AREA NUMBER
I created a dynamic action from my page, in order to take the above values from CLIENTS table according to my :P2007_CLIENTS_ID.
First i created this type:
CREATE OR REPLACE EDITIONABLE TYPE "PATIENT_DETAILS" as object
( payment_type number,
session_area number,
price number
)
/
then i created this function:
create or replace FUNCTION "F_PATIENT_DETAILS"
(patient_id in NUMBER, session_date date)
RETURN patient_details
IS
v_payment_type number;
v_session_area number;
v_price number;
BEGIN
SELECT CLIENTS.PAYMENT_TYPE_ID into v_payment_type
FROM CLIENTS
WHERE CLIENTS.ID = patient_id;
SELECT CLIENTS.SESSION_AREA into v_session_area
FROM CLIENTS
WHERE CLIENTS.ID = patient_id;
SELECT CLIENTS.PRICE into v_price
FROM CLIENTS
WHERE CLIENTS.ID = patient_id;
if v_price is null then
SELECT POLICIES.PRICE into v_price
FROM POLICIES
WHERE POLICIES.ACTIVE = 1
AND to_char(session_date, 'MM-DD-YYYY') BETWEEN POLICIES.START_DATE AND POLICIES.END_DATE;
end if;
return patient_details(v_payment_type, v_session_area, v_price);
END;
How do i get the values from this function in my page, with Dynamic Action?
I tried this: Identification-> Set Value, Set Type -> PL/SQL Function Body:
declare
My_Result PATIENT_DETAILS;
begin
My_Result := F_PATIENT_DETAILS(:P2007_CLIENTS_ID, :P2007_SESSION_DATE);
end;
Items to Submit-> P2007_CLIENTS_ID, :P2007_SESSION_DATE
Affected Elements -> P2007_PAYMENT_TYPE_ID, :P2007_SESSION_AREA, :P2007_PRICE
but nothing happens..!
Those three fields are never assigned the new values after your function is returned, eg:
:P2007_PAYMENT_TYPE_ID := my_result.payment_type;
Also, there is no reason for 3 separate queries on CLIENTS. You could do this in one motion.
SELECT c.PAYMENT_TYPE_ID, c.SESSION_AREA, c.PRICE
into v_payment_type, v_session_area, v_price
FROM CLIENTS c
WHERE c.ID = patient_id;
Taking that a step further, you could coalesce c.price with a subquery on policies.
Which has a questionable filter on a date being represented as a character. I doubt this would return accurate results.

PLS-00487 Error-Invalid reference to Variable 'CHAR'

I'm designing a function that is part of a larger package. The function is intended to take a District Code and return a collection of unique IDs for 10-15 stores that are assigned to that district. The function is intended to return a collection that can be queried like a table, i.e., using the TABLE function in a SQL statement.
I've created the following Types:
Schema Level type:
create or replace TYPE HDT_CORE_ORGIDS AS TABLE OF CHAR(20);
and a Type inside the Package
TYPE CORE_ORGIDS IS TABLE OF CHAR(20) INDEX BY BINARY_INTEGER;
Here's the function code:
FUNCTION FindDistrictOrgs(
ParamOrgCode VARCHAR2
)
RETURN HDT_CORE_ORGIDS
AS
ReturnOrgs HDT_CORE_ORGIDS := HDT_CORE_ORGIDS();
FDOTemp HDT_CORE_MAIN.CORE_ORGIDS;
i BINARY_INTEGER := 0;
CURSOR FDOCurr IS
SELECT org.id AS OrgID
FROM tp2.tpt_company org
WHERE LEVEL = 2
START WITH org.name = ParamOrgCode
CONNECT BY PRIOR org.id = org.parent_id;
BEGIN
OPEN FDOCurr;
LOOP
i := i +1;
FETCH FDOCurr INTO FDOTemp(i);
EXIT WHEN FDOCurr%NOTFOUND;
END LOOP;
IF FDOTemp.EXISTS(FDOTemp.FIRST) THEN
ReturnOrgs.EXTEND(FDOTemp.LAST);
FOR x IN FDOTemp.FIRST .. FDOTemp.LAST LOOP
ReturnOrgs(x) := FDOTemp(x).OrgID;
END LOOP;
END IF;
CLOSE FDOCurr;
RETURN ReturnOrgs;
END FindDistrictOrgs ;
I'm getting the PLS-00487:Invalid Reference to variable 'CHAR' at the line:
ReturnOrgs(x) := FDOTemp(x).OrgID;
I've double-checked at the value returned by the SQL (the org.id AS OrgID) is of the CHAR(20 BYTE) datatype.
So...what's causing the error?
Any help is appreciated! :)
OrgID is the alias you gave the column in your cursor, it has no meaning to the collection. Since both collections are of simple types you should just be doing:
ReturnOrgs(x) := FDOTemp(x);
The syntax you're using is implying FDOTemp is a collection of objects and you're trying to reference the OrgID attribute of an object; but since CHAR isn't an object type, this errors. The error message even makes some sense when viewed like that, though it's not terribly helpful if you don't already know what's wrong... and not entirely helpful when you do.
Incidentally, you could use a bulk collect to populate the collection without the cursor or loops, or the extra collection:
SELECT org.id
BULK COLLECT INTO ReturnOrgs
FROM tp2.tpt_company org
WHERE LEVEL = 2
START WITH org.name = ParamOrgCode
CONNECT BY PRIOR org.id = org.parent_id;
RETURN ReturnOrgs;

oracle pl sql function having errors

I want to create a function that returns the number of rows in a table called Rating with a where clause.Where am i going wrong before the declare statement and the end statement?
create or replace
FUNCTION get_movies(user IN NUMBER) RETURN NUMBER
IS
DECLARE cnt NUMBER;
BEGIN
SELECT count(*)
INTO cnt
FROM rating
where userid= user;
RETURN cnt;
END;
I will appreciate help.Thanks.
You should not have the DECLARE keyword. You only need that for an anonymous block (or a sub-block).
create or replace
FUNCTION get_movies(p_userid IN NUMBER) RETURN NUMBER
IS
cnt NUMBER;
BEGIN
...
user is a reserved word so I'd suggest not using that as your parameter name. In the where clause I'm not sure if it will use your parameter value, or the name of the user executing the function; which would error as that string value couldn't be implicitly converted to a number.

how to return a cursor or a result set from a oracle stored function

I have a stored function
CREATE OR REPLACE FUNCTION schedule(name in varchar2,pass in varchar2 )
begin
select t.name,s.starttime from traininfo t,schedule s, trainslot ts
where t.trainid in( select ts.trainid from trainslot
where ts.slotid in (select s.slotid from schedule s
where s.source='dhaka'
and s.dest='bogra' ))
end
I want to return this result set using a cursor.
I don't see where you are using either of the input parameters in your function. I'll assume that is either intentional or an oversight because you're simplifying the code. It also appears that your query is missing conditions to join between the traininfo, schedule, and trainslot tables in the outer query. It seems odd that your nested IN statements are turning around and querying the schedule and trainslot tables given this lack of join conditions. I don't know whether this is a result of copy-and-paste errors or something that was missed in posting the question or whether these are real problems. I'll make a guess at the query you're intending to write but if my guess is wrong, you'll have to tell us what your query is supposed to do (posting sample data and expected outputs would be exceptionally helpful for this).
CREATE OR REPLACE FUNCTION schedule(name in varchar2,pass in varchar2 )
RETURN sys_refcursor
is
l_rc sys_refcursor;
begin
open l_rc
for select t.name, s.starttime
from traininfo t,
schedule s,
trainslot ts
where t.trainid = ts.trainid
and s.slotid = ts.slotid
and s.source = 'dhaka'
and s.dest = 'borga';
return l_rc;
end;

Resources