Plsql Procedure in invalid state - oracle

I have created following procedure:
create or replace function getDepartmentById(name varchar2)
return number
is v_dep number(10)
begin
--the sql statement is totally fine
select deptno into v_dep from dept where dname = name;
end;
/
but when I call
select getDepartmentById('SALES') into dep from dual;
I get this error message
Error report -
ORA-06550: line 20, column 14:
PL/SQL: ORA-06575: Package or function GETDEPARTMENTBYID is in an invalid state
ORA-06550: line 20, column 7:
PL/SQL: SQL Statement ignored

You need to add a return value for the function. It goes like this :
CREATE OR replace FUNCTION Getdepartmentbyid(name VARCHAR2)
RETURN NUMBER
IS
v_dep NUMBER(10);
BEGIN
--the sql statement is totally fine
SELECT deptno
INTO v_dep
FROM dept
WHERE dname = name;
RETURN v_dep;
END;
You can not use a INTO in your select statement. You should call the function like this.
select getDepartmentById('SALES') from dual;

Related

How to get the number of rows affected by an UPDATE statement

I am trying to get the number of rows affected by an UPDATE statement, however I am getting the following error:
ORA-06550: line 2, column 9:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table columns long
double ref char time timestamp interval date binary national
character nchar
The symbol "exception" was substituted for ";" to continue.
The query that I am executing is the following:
DECLARE
var_rows;
BEGIN
var_rows:=SQL%ROWCOUNT;
UPDATE {Product} SET {Product}.[Name] = concat({Product}.[Name], ' - test') WHERE {Product}.[ProductTypeId] = 2;
SELECT var_rows FROM DUAL;
END;
Use SQL%rowcount properly. i.e. immediately after the update
Example
create table tab1 as
select 1 id, 'a' col from dual union all
select 1, 'b' from dual;
set serveroutput on;
declare
rn int;
begin
update tab1
set col = 'z';
rn := SQL%rowcount;
dbms_output.put_line(rn);
end;
/
2
PL/SQL procedure successfully completed.

Oracle pl/sql problem when trying to reference plsql table in query

I am getting a pls-201 error when I try to reference a plsql table record in a query.
Here is an example of the issue:
DECLARE
TYPE trxtypeinforec IS RECORD(
NAME ra_cust_trx_types.NAME%TYPE
);
TYPE trxtypeinfotab IS TABLE OF trxtypeinforec
INDEX BY PLS_INTEGER;
g_inv_type trxtypeinfotab;
l_result VARCHAR2 (100);
BEGIN
g_inv_type(1).NAME := 'Test';
SELECT g_inv_type(qry.ID).NAME
INTO l_result
FROM (SELECT 1 ID
FROM dual) qry;
END;
Error report - ORA-06550: line 15, column 23: PLS-00201: identifier
'QRY.ID' must be declared ORA-06550: line 15, column 23: PLS-00201:
identifier 'QRY.ID' must be declared ORA-06550: line 15, column 12:
PL/SQL: ORA-00904: : invalid identifier ORA-06550: line 15, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Retrieve the index from the SELECT first into a local variable and then assign the value from the associative array to l_result in a PL/SQL scope and not in an SQL scope:
DECLARE
TYPE trxtypeinforec IS RECORD(
NAME ra_cust_trx_types.NAME%TYPE
);
TYPE trxtypeinfotab IS TABLE OF trxtypeinforec INDEX BY PLS_INTEGER;
g_inv_type trxtypeinfotab;
idx PLS_INTEGER;
l_result ra_cust_trx_types.NAME%TYPE;
BEGIN
g_inv_type(1).NAME := 'Test';
SELECT 1
INTO idx
FROM DUAL;
l_result := g_inv_type(idx).NAME;
DBMS_OUTPUT.PUT_LINE( l_result );
END;
/
Outputs:
Test
db<>fiddle here
Please try using a cursor.
SET SERVEROUTPUT ON;
DECLARE
TYPE trxtypeinforec IS RECORD(
NAME ra_cust_trx_types.NAME%TYPE
);
TYPE trxtypeinfotab IS TABLE OF trxtypeinforec
INDEX BY PLS_INTEGER;
g_inv_type trxtypeinfotab;
l_result VARCHAR2 (100);
-- Query for retrieving index goes in the cursor or use existing cursor
CURSOR cur_id IS
SELECT 1 AS ID FROM DUAL;
BEGIN
-- Populate records here or put inside loop as required
g_inv_type(1).NAME := 'Test';
-- Loop through the cursor to get the relevant index
FOR rec in cur_id
LOOP
SELECT g_inv_type(rec.ID).NAME
INTO l_result
FROM DUAL;
dbms_output.put_line (l_result);
END LOOP;
END;
/
It will be helpful if we could get some more information on how the existing cursor is fetching index values.
Output:
Test
PL/SQL procedure successfully completed.

ORA-00932 facing while compiling the oracle function

I have made below code.
CREATE OR REPLACE TYPE CAL IS OBJECT(
EMPLOYEE_NAME VARCHAR2(30),
R_DATE DATE,
COMMENTS VARCHAR2(50)
);
CREATE OR REPLACE TYPE T_REC is table of cal;
create or replace function CALENDAR(v_team_name varchar2)
return t_rec
IS
v_rec t_rec;
v_COMM VARCHAR2(50);
v_name VARCHAR2(30);
BEGIN
FOR i in (Select ID,EMPLOYEE_NAME from EMPLOYEE where TEAM_NAME=v_team_name)
LOOP
v_name:=i.EMPLOYEE_NAME;
FOR k in (select EMP_ID, START_DT,END_DT-START_DT+1 DAYS,NAME,COMMENTS from EMP_ROTA where EMP_ID=i.ID)
LOOP
v_COMM:=k.NAME||', '||k.COMMENTS;
select t_rec(v_name,k.START_DT+level-1,v_COMM)
into v_rec
from dual connect by level < k.DAYS;
END LOOP;
END LOOP;
Return v_rec;
END;
Facing below error in compiling the function.
Compilation failed,line 14 (04:33:54)
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got CHARCompilation failed,line 14 (04:33:54)
PL/SQL: SQL Statement ignored
While using below query in function
select v_name,k.START_DT + level -1,v_COMM
into v_REC
from dual connect by level < k.DAYS;
Facing below error
PL/SQL: ORA-00947: not enough valuesCompilation failed,line 14 (04:50:49)
Trying small snippet of code stil facing ORA-00932.
create or replace function ROTA_CALENDAR
return t_CAL
IS
v_CAL t_CAL;
BEGIN
select t_cal('v_name',SYSDATE,'NAME')
into v_CAL
from dual;
Return v_CAL;
END;
what am I doing wrong?
I hope below snippet will help. Couple of things to take in consideration.
1 Don't use "" while any nomenclature
2 You have to select from Object type in the SELECT query as mentioned below.
3 Use BULK COLLECT instead of INTO only...
CREATE OR REPLACE
FUNCTION CALENDAR(
V_TEAM_NAME VARCHAR2)
RETURN t_rec
IS
v_rec t_rec;
v_COMM VARCHAR2(50);
v_name VARCHAR2(30);
BEGIN
FOR i IN
(
SELECT
EMPNO,
'AVRAJIT' ENAME
FROM
EMP
WHERE
JOB=v_team_name
)
LOOP
v_name:=i.ENAME;
FOR k IN
(
SELECT
EMPNO,
SYSDATE,
SYSDATE+1 DAYS,
ENAME,
JOB
FROM
EMP_V1
WHERE
EMPNO=i.EMPNO
)
LOOP
v_COMM:=k.ENAME||', '||k.JOB;
SELECT
CAL(V_NAME,SYSDATE+1,V_COMM) BULK COLLECT
INTO
v_rec
FROM
dual
CONNECT BY level < 10;
END LOOP;
END LOOP;
RETURN v_rec;
END;
-------------------------------OUTPUT-------------------------------------------
SELECT OBJECT_NAME,STATUS FROM ALL_OBJECTS
WHERE OBJECT_NAME = 'CALENDAR';
OBJECT_NAME STATUS
CALENDAR VALID
-------------------------------OUTPUT-------------------------------------------

passing in table name as plsql parameter

I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:
create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;
I get this error:
FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist
I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.
You can use dynamic SQL:
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
execute immediate 'select count(*) from ' || table_name into table_count;
dbms_output.put_line(table_count);
return table_count;
end;
There is also an indirect way to get number of rows (using system views):
create or replace function get_table_count (table_name IN varchar2)
return number
is
table_count number;
begin
select num_rows
into table_count
from user_tables
where table_name = table_name;
return table_count;
end;
The second way works only if you had gathered statistics on table before invoking this function.

PL/SQL statement ignored, cannot display

I keep getting these problems when all I wanted was to display the maximum salary of an employee in a job id
ORA-06550: line 12, column 6:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 8, column 6:
PL/SQL: SQL Statement ignored
Here's my code:
DECLARE
lvc_jb_id VARCHAR2;
lvn_max_sal NUMBER;
BEGIN
SELECT max(salary), job_id
INTO lvn_max_sal, lvc_jb_id
FROM EMPLOYEES
group by job_id
DBMS_OUTPUT.PUT_LINE('MAX SALARY for job_id is'|| lvn_max_sal);
DBMS_OUTPUT.PUT_LINE('job id '|| lvc_jb_id);
END;
Can anyone tell what I did wrong?
Specify the size of the string.
eg: lvc_jb_id VARCHAR2(50);
End the select query with semicolon. Now it should work.

Resources