runtime plsql throwing table not exist error - oracle

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.

Related

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.

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;

'[Error] Execution (8: 3): ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 5' in the following plsql

thanks for all,i had modified from varchar2(10) to varchar2(20) and removing the column keyword in execute immediate statement then program is executing
declare
coldate varchar2(20);
colname varchar2(20);
begin
coldate :='varchar2(10)';
colname :='smaple';
execute immediate 'alter table smap1 add column '||colname ||' '||coldate ;
end;
if i want to take the values dynamically i had used the following code
declare
coldate varchar2(20):=&coldate;
colname varchar2(20):=&colname;
begin
execute immediate 'alter table smap1 add '||colname ||' '||coldate ;
end;
then i am getting errors
[Error] Execution (11: 23): ORA-06550: line 2, column 23:
PLS-00330: invalid use of type name or subtype name
ORA-06550: line 2, column 9:
PL/SQL: Item ignored
ORA-06550: line 3, column 23:
PLS-00201: identifier 'SMAPLE' must be declared
ORA-06550: line 3, column 9:
PL/SQL: Item ignored
ORA-06550: line 6, column 45:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
declare
coldate varchar2(20);
colname varchar2(20);
begin
coldate :='varchar2(10)';
colname :='smaple';
execute immediate 'alter table smap1 add column '||colname ||' '||coldate ;
end;
This query wont work as syntactically its not correct.
Please try this as it will help you definitely
declare
coldate varchar2(20):='varchar2(20)';
colname varchar2(20):='Newadd';
begin
execute immediate 'alter table <tablename> add '||colname ||' '||coldate ;
end;
-- And to use dynamically input values Please try this
SET DEFINE ON;
declare
coldate varchar2(20):='&DATATYPE';
colname varchar2(20):='&COL_NAME';
begin
execute immediate 'alter table emp add '||colname ||' '||coldate ;
end;
The below will solve it:
coldate varchar2(12);
colname varchar2(20);
begin
coldate :='varchar2(10)';

Missing keyword error in Oracle anonymous PL/SQL block

An anonymous PL/SQL block:
DECLARE
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table
BEGIN
FOR employee_rec
IN employees_in_10_cur
LOOP
DBMS_OUTPUT.put_line(employee_rec.ad_no || ',' || employee_rec.week_no);
END LOOP;
END;
I get a missing keyword error when I execute this block. What am I doing wrong?
put a semi-colon after defining cursor query;
DECLARE
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table;
BEGIN
FOR employee_rec
IN employees_in_10_cur
LOOP
DBMS_OUTPUT.put_line (
employee_rec.ad_no || ',' || employee_rec.week_no );
END LOOP;
END;
You're missing a semi-colon at the end of your cursor:
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table;
The error message will always give you the line number of the error occurring. Please always check this and look in that area.
For instance; if I change the table so I can run this I get the following:
SQL> declare
2
3 cursor employees_in_10_cur is
4 select *
5 from user_tables
6
7 begin
8 for employee_rec in employees_in_10_cur loop
9 dbms_output.put_line (
10 employee_rec.ad_no || ',' || employee_rec.week_no );
11 end loop;
12 end;
13 /
for employee_rec in employees_in_10_cur loop
*
ERROR at line 8:
ORA-06550: line 8, column 8:
PL/SQL: ORA-00905: missing keyword
ORA-06550: line 4, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 11, column 4:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
You have 3 errors; one at line 8 because the cursor is invalid, one at line 11 because the loop doesn't happen because the cursor is invalid and one at line 4, which says "statement ignored".

Resources