Compilation error in Oracle PL/SQL - sql

I have been staring at this hours trying to figure out why it won't run, and cannot see any syntax error which should stop this from compiling, but I am nonetheless getting the following errors:
Error(6,1): PL/SQL: SQL Statement ignored
Error(22,7): PL/SQL: ORA-00933: SQL command not properly ended
The function I am trying to create is as follows:
create or replace function caps_get_Uoffer_count_BD(ayrc in varchar2
,mcrc in varchar2)
return number
is
app_count number;
begin
select count(*)
from
(select distinct cap.cap_stuc,cap.cap_apfs , cap.cap_seqn from
intuit.srs_cap cap
,intuit.srs_apf apf
,intuit.srs_ioi ioi
where cap.cap_ayrc = ayrc
and cap.cap_mcrc = mcrc
and apf.apf_stuc = cap.cap_stuc
and apf.apf_seqn = cap.cap_apfs
and apf.apf_recd <= to_date('1501'||substr(ayrc,1,4),'DDMMYYYY')
and cap.cap_stuc = ioi.ioi_stuc
and cap.cap_mcrc = ioi.ioi_mcrc
and ioi.ioi_iodc ='PAPERLESS'
and cap.cap_stac like 'A%'
and cap.cap_idrc in ('U','UF','UI','UD','CFU','CFUF','CIUI','CIUF','CIUD','CIU','CFUI','CFUD'))
return(app_count);
end;
Database is Oracle 11g. Can anyone help?
Many thanks in advance

Related

Oracle function compiles successfully but throws error while executing PLS-00221: is not a procedure or is undefined

I have simple oracle function
create or replace function abs.test_func(test_in in number)
return number
is
test_out number ;
BEGIN
test_out:=test_in;
RETURN test_out;
END;
if I compile it - it compiles successfully.
but when I run from PLSQL Developer SQL Window
BEGIN abs.test_func(5); END;
it I gets the following errors
ORA-06550: line1, column8;
PLS-00221: 'TEST_FUNC' is not a procedure or is undefined
ORA-06550: line1, column8;
PL/SQL: Statement ignored
What's wrong with my function ?
Your create function code looks good, however you are not invoking the function properly. A function returns something, that you must either select, assign, print, or evaluate.
Here are a few examples of valid function calls:
-- print the return value
begin
dbms_output.put_line(test_func(5));
end;
/
1 rows affected
dbms_output:
5
-- select the return value
select test_func(5) from dual;
| TEST_FUNC(5) |
| -----------: |
| 5 |
Demo on DB Fiddle

PL/SQL and SQL Developer different results from each other

I am executing a query in PL / SQL in version 7 and version 14, with a function created by me, and both bring me some results, the rest bring 0.
However, when executing the same query in Oracle SQL Developer, the query brings all the results correctly.
I executed the procedure through PL / SQL and Oracle SQL Developer as well, but then none brought me the right result, all the lines were left as "0".
I can't find the problem at all, even on Google.
Basically, the function multiplies the number of rows by columns that start with "ID_", as shown below.
Function:
CREATE OR REPLACE FUNCTION DS_FUNCESP.FNBIGB_CheckDataCells
(pOwn IN VARCHAR2,
pTab IN VARCHAR2)
RETURN NUMBER
IS
v_Qtd NUMBER;
v_str VARCHAR2(2000);
BEGIN
v_Qtd := 1;
v_str := ' SELECT
SUM((SELECT COUNT(1) AS QTY_ROWS FROM ' || pOwn || '.' || pTab || ' d WHERE d.LINORIGEM <> ''CARGA MANUAL'')) AS QTY_DATA
FROM DW_FUNCESP.D_BI_COLUMNS a
LEFT JOIN
DW_FUNCESP.D_BI_TABLES b
ON a.ID_TABLE = b.ID_TABLE
AND a.ID_OWNER = b.ID_OWNER
LEFT JOIN DW_FUNCESP.D_BI_OWNERS c
ON a.ID_OWNER = c.ID_OWNER
WHERE b.NM_TABLE = ''' || pTab || '''
AND a.IN_PRIMARYKEY = ''NAO''
AND SUBSTR(a.NM_COLUMN,1,3) = ''ID_'' ';
DBMS_OUTPUT.put_line(v_str);
EXECUTE IMMEDIATE v_str into v_Qtd ;
return (v_Qtd);
EXCEPTION WHEN OTHERS THEN
RETURN 0;
END FNBIGB_CheckDataCells;
Select statement:
SELECT
c.NM_OWNER ,
b.NM_TABLE ,
DS_FUNCESP.FNBIGB_CHECKDATACELLS(c.NM_OWNER, b.NM_TABLE) AS QTY_DATA
FROM DW_FUNCESP.D_BI_TABLES b
LEFT JOIN DW_FUNCESP.D_BI_OWNERS c
ON b.ID_OWNER = c.ID_OWNER;
Results from PL/SQL:
Results from Oracle SQL Developer:
Clearly we can see the difference from any row, the right one is the Oracle SQL Developer. So I'd like to know what is the problem, how to fix, because the procedure is adding "0" to all the rows, no matter where I run.
Reading those examples from WHEN OTHERS - A Bug, thanks to #Lalit Kumar B for that, I changed:
EXCEPTION WHEN OTHERS THEN
RETURN 0;
To:
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQLCODE: '||SQLCODE);
DBMS_OUTPUT.PUT_LINE('Message: '||SQLERRM);
RAISE;
To find out the problem, and thanks for that I found that it was trying to count from a table where it doesn't exist anymore.
So I using an error handling as below, from #Jeffrey Kemp
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
Also, thanks for #Belayer, my code was the problem, agreed on that. Also, executing on both softwares, made me even more confused. I'll read also that documentation for sure.

Creating a Procedure in Oracle to create View

I am trying to create a procedure in oracle, which upon calling from PL SQL block will create a view in database from which i will query data for a report. I am new to Oracle and need help with this code.
CREATE OR REPLACE PROCEDURE CREATE_VIEW
(
TO_DT IN Date
) AS
BEGIN
Create or Replace view BORR_DUR As
SELECT e."Deal_No", (Select "DeskName" From MM_S_DESK Where e."DeskCode" = MM_S_DESK."DeskCode") Facility, e."Remarks" Counterparty,
m."MaturityDate", m."PriRedem" Principal,
(select MAX("INTEREST_RATE") from MM_BOR_PLA_PAR d
WHERE e."Deal_No" = d."DEAL_NO" and "INTERESTINPUTDATE" <= to_dt)/100 yield, (m."MaturityDate" - To_date(to_dt,'dd/mm/yyyy')) Days_to_Mat,
Round(((m."MaturityDate" - To_date(to_dt,'dd/mm/yyyy'))/365)/ (1+((select MAX("INTEREST_RATE") from MM_BOR_PLA_PAR d
WHERE e."Deal_No" = d."DEAL_NO" and "INTERESTINPUTDATE" <= to_dt)/100)),4) MDURATION
FROM MM_T_BORROWING e, MM_T_BORROWING_PM_DETAIL m
Where e."DeskCode" in ('10','11','12','13') and e."Value_Date" <= to_dt and e."Maturity_Date" > to_dt and e."Status" not in ('C', 'D', 'Z', '0','X')
and e."Deal_No" = m."Deal_No" and "PriRedem" > '0' and m."MaturityDate" > to_dt;
END CREATE_VIEW;
On Compilation, i get PLS00103 error which says
encountered the symbol "Create" when expecting one of the
following....
Any help in solving this issue will be greatly appreciated.
When you want execute SQL statement which is dynamic you have to use EXECUTE IMMEDIATE statement
First , you don't need double quotes in fields name , after that you can try the query of the view and check if it runs without errors .
Put the create replace view... statement in an variable and in your procedure call :
BEGIN
EXECUTE IMMEDIATE view_string_variable ;
END;
/

PL/SQL function error- not understandable

I have to do a pl/sql function. I have quite done it but on the server it shows an error but I have no idea what does the error means.
Could someone guide me into the right direction to complete the function?
This is the code:
SHOW ERRORS
CREATE OR REPLACE FUNCTION sum_of_task_types(project project.project_no%TYPE)
RETURN NUMBER IS
task NUMBER;
BEGIN
SELECT COUNT(DISTINCT t.task_type_no)
INTO task
FROM project p, stage s, task t ;
WHERE user_input_var = p.project_no;
AND p.project_no = s.project_no;
AND s.stage_id = t.stage_id;
AND p.project = project;
RETURN task;
END;
/
--use of the function
SELECT project_no, sum_of_task_types(project_no)project;
I do not know if the code looks clear here, so I have added a document showing the image of the code itself with the error shown below:
CREATE OR REPLACE FUNCTION sum_of_task_types(project project.project_no%TYPE)
RETURN NUMBER IS
task NUMBER;
BEGIN
SELECT COUNT(DISTINCT t.task_type_no)
INTO task
FROM project p, stage s, task t
WHERE user_input_var = p.project_no
AND p.project_no = s.project_no
AND s.stage_id = t.stage_id
AND p.project = project
RETURN task;
END;
/
I have done this now, still gives errors:
LINE/COL ERROR
6/5 PL/SQL: SQL Statement ignored
13/5 PL/SQL: ORA-00933: SQL command not properly ended
just add a ';' after 'AND p.project = project' to indicate termination of sql statement and this will become error free.
--There was a slight ';' syntactical mistake which was in the way of compiling FUNCTION correctly. I have posted below code hope it helps
CREATE OR REPLACE FUNCTION sum_of_task_types(
project project.project_no%TYPE)
RETURN NUMBER
IS
task NUMBER;
BEGIN
SELECT COUNT(DISTINCT t.task_type_no)
INTO task
FROM project p,
stage s,
task t
WHERE user_input_var = p.project_no
AND p.project_no = s.project_no
AND s.stage_id = t.stage_id
AND p.project = project;
RETURN task;
END;

Issue with dynamic execute immediate query

I have a code in my procedure that looks like this. But when i execute this code, i get the error as mentioned below.
The Error report that i got is:
Error report -
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_Y'
ORA-06512: at line 20
06553. 00000 - "PLS-%s: %s"
*Cause:
*Action:
The error has something to do with primary_flag = "Y" <-- this. How else can i write primary_flag = 'Y' inside a string?
The dynamic query is required in my case.
MY CODE IS:
DECLARE
p_assignee_id NUMBER := 10153;
time_stamp timestamp := '12-DEC-2011';
create_task_view_sql VARCHAR2(4000);
BEGIN
create_task_view_sql:=
'select unique cp.sub_last_name
from cs_sr_contact_points_v cp
where cp.incident_id = 55500
and cp.contact_phone is not null
and primary_flag = "Y"';
dbms_output.put_line(create_task_view_sql);
execute immediate create_task_view_sql;
END;
To embed quoted string in a quoted string, use two single quotes:
'...and primary_flag=''Y''';
Or you can use the newer q' syntax to avoid doubling up the embedded quotes:
q'[...and primary_flag='Y']';