PL/SQL statement ignored, cannot display - oracle

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.

Related

Plsql Procedure in invalid state

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;

PL/SQL: ORA-00933: SQL command not properly ended in the cursor SQL developer

#C:\Users\4\Desktop\dbdrop;
#C:\Users\4\Desktop\dbcreate;
SET SERVEROUTPUT ON;
DECLARE
ORDER_ID ORDERS.ODID%TYPE;
COMPANY_NAME ORDERS.CNAME%TYPE;
ORDER_DATE ORDERS.ODATE%TYPE;
CURSOR ord_cursor IS
SELECT ODID, CNAME, ODATE
FROM ORDERS
WHERE ODER_DATE< CURDATE()
LIMIT 5;
BEGIN
OPEN ord_cursor;
LOOP
FETCH ord_cursor into ORDER_ID, COMPANY_NAME, ORDER_DATE;
DBMS_OUTPUT.PUT_LINE(' ');
DBMS_OUTPUT.PUT_LINE('ODER ID: '|| TO_CHAR(Order_Id));
DBMS_OUTPUT.PUT_LINE( 'ODER DATE: ' || ORDER_DATE );
DBMS_OUTPUT.PUT_LINE('COMPANY NAME: '|| COMPANY_NAME );
DBMS_OUTPUT.PUT_LINE( '------------');
DBMS_OUTPUT.PUT_LINE( '------------');
IF emp_cursor%NOTFOUND THEN
EXIT;
END IF;
END LOOP;
CLOSE ord_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
And the error shows:
Error report -
ORA-06550: line 9, column 13:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 6, column 9:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

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.

runtime plsql throwing table not exist error

I have below code which checks for table exists & proceed further.
Here problem is why its proceeding to else part even though table is not present in the database & also its returning "0"
SQL> SET serveroutput ON trimspool on feed off echo on
declare
c_cnt number;
t_cnt number;
begin
select count(1) into t_cnt from dba_tables where owner='PRODDBA' and table_name='IOT_LIST';
dbms_output.put_line(t_cnt);
if t_cnt = 0 then
dbms_output.put_line('NO_ACTION');
else
select count(1) into c_cnt from PRODDBA.IOT_LIST;
if c_cnt = 0 then
dbms_output.put_line('NO_ACTION');
else
for i in (select temp_table_name from PRODDBA.IOT_LIST)
loop
begin
dbms_output.put_line(i.temp_table_name);
execute immediate 'drop table PRODDBA.'||i.temp_table_name||' purge';
EXCEPTION WHEN OTHERS then
CONTINUE;
end;
end loop;
end if;
end if;
end;
/
select count(1) into c_cnt from PRODDBA.IOT_LIST;
ERROR at line 10:
ORA-06550: line 10, column 41:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 10, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 14, column 47:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 14, column 11:
PL/SQL: SQL Statement ignored
ORA-06550: line 17, column 22:
PLS-00364: loop index variable 'I' use is invalid
ORA-06550: line 17, column 1:
PL/SQL: Statement ignored
ORA-06550: line 18, column 42:
PLS-00364: loop index variable 'I' use is invalid
ORA-06550: line 18, column 1:
PL/SQL: Statement ignored
You should make use of dynamic REFCURSOR to run a loop to drop tables dynamically.
SET SERVEROUTPUT ON
DECLARE
c_cnt NUMBER;
t_cnt NUMBER;
v_schema_name VARCHAR2(40) := 'PRODDBA';
v_table_name VARCHAR2(40) := 'IOT_LIST';
v_tabs VARCHAR2(40);
refcur SYS_REFCURSOR;
BEGIN
SELECT COUNT(1)
INTO t_cnt
FROM dba_tables
WHERE owner = v_schema_name
AND table_name =v_table_name;
dbms_output.put_line(t_cnt);
IF t_cnt = 0 THEN dbms_output.put_line('NO_ACTION');
ELSE
OPEN refcur FOR 'SELECT temp_table_name
FROM '||v_schema_name||'.'||v_table_name;
LOOP
BEGIN
FETCH refcur INTO v_tabs;
EXIT WHEN refcur%NOTFOUND;
EXECUTE IMMEDIATE 'drop table '||v_schema_name||'.'
|| v_tabs
|| ' purge';
dbms_output.put_line('DROPPED ' || v_tabs);
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line( 'TABLE NOT FOUND: '||v_tabs);
CONTINUE;
END;
END LOOP;
IF refcur%ROWCOUNT = 0 THEN
dbms_output.put_line('NO_ACTION');
END IF;
END IF;
END;
/
Result
Before table creation
<execute the block>
0
NO_ACTION
PL/SQL procedure successfully completed.
After table creation
create table IOT_LIST ( temp_table_name varchar2(40));
INSERT INTO IOT_LIST values('T1');
INSERT INTO IOT_LIST values('T2');
INSERT INTO IOT_LIST values('T4');
<execute the block>
1
DROPPED T1
DROPPED T2
TABLE NOT FOUND: T4
PL/SQL procedure successfully completed.

plsql comparing table and view

I need to get comparison of columns between table and a view i choose to see if there are matching columns.(in PLSQL)
The columns that don't match need to be outputed as: column1 in view1 is missing in table1 etc.
This is so far i have done, but it gives me an error:
DECLARE
CURSOR c_col
IS
SELECT T.TABLE_NAME,
T.COLUMN_NAME,
V.TABLE_NAME,
V.COLUMN_NAME
FROM ALL_TAB_COLUMNS T
FULL JOIN ALL_TAB_COLUMNS V
ON T.column_name=V.column_NAME
AND v.table_name='EMP_V'
AND v.owner ='HR'
WHERE T. OWNER ='HR'
and T.TABLE_NAME='EMPLOYEES';
v_table c_col%rowtype;
begin
OPEN C_col;
LOOP
FETCH C_col into V_TABLe;
EXIT when C_col%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (v_table.table_name||' '||V_table.column_name);
end LOOP;
close c_col;
end;
this is the error i keep getting:
Error report -
ORA-06550: line 16, column 13:
PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
ORA-06550: line 16, column 13:
PL/SQL: Item ignored
ORA-06550: line 20, column 22:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 20, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 22, column 27:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 22, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Try this, for the error cause see the #Multisync comment
declare
cursor c_col is
select T.TABLE_NAME, T.COLUMN_NAME, V.TABLE_NAME VIEW_NAME,
V.COLUMN_NAME VIEW_COLUMN_NAME
from (select *
from ALL_TAB_COLUMNS T
where OWNER = 'HR'
and TABLE_NAME = 'EMPLOYEES') T
full outer join (select *
from ALL_TAB_COLUMNS
where owner = 'HR'
and table_name = 'EMP_V') V on T.column_name =
V.column_NAME
order by t.column_name, v.column_name;
v_table c_col%rowtype;
begin
open C_col;
loop
fetch C_col
into V_TABLe;
exit when C_col%notfound;
DBMS_OUTPUT.PUT_LINE(rpad(v_table.table_name || ' ' ||
V_table.column_name, 30, ' ') || '| ' ||
v_table.VIEW_NAME || ' ' ||
V_table.VIEW_COLUMN_NAME);
end loop;
close c_col;
end;

Resources