plsql comparing table and view - oracle

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;

Related

PL/SQL -- substitution variable

I am using SQL Developer and writing this PL/SQL code, but I am getting error. Please help.
SET SERVEROUTPUT ON
ACCEPT name prompt 'Name of employee'
declare
v_sal NUMBER;
BEGIN
v_sal := &name;
SELECT
salary
INTO v_sal
FROM
employees
WHERE
first_name = '&name';
dbms_output.put_line('Employee ' || &name || ' has the salary' || v_sal);
END;
////
Error report -
ORA-06550: linia 4, coloana 14:
PLS-00201: identifier 'MOZHE' must be declared
ORA-06550: linia 4, coloana 5:
PL/SQL: Statement ignored
ORA-06550: linia 12, coloana 54:
PLS-00201: identifier 'MOZHE' must be declared
ORA-06550: linia 12, coloana 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
You want:
SET SERVEROUTPUT ON
ACCEPT name prompt 'Name of employee'
DECLARE
v_sal NUMBER;
BEGIN
SELECT salary
INTO v_sal
FROM employees
WHERE first_name = '&&name';
dbms_output.put_line('Employee &&name has the salary' || v_sal);
END;
/

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:

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.

How do I declare a query in an oracle pl/sql statement?

I'm fairly new to Oracle but Ive been tasked to write this statement .
BEGIN
FOR partition IN 1..32 LOOP
lQuery := 'UPDATE CORE.tbl PARTITION(tbl' || LPAD(partition, 2, '0') || ') SET p = NULL '
|| 'WHERE p IS NOT NULL';
EXECUTE IMMEDIATE lQuery;
END LOOP;
END;
However, I get the following error
Error starting at line 1 in command:
BEGIN
FOR partition IN 1..32 LOOP
lQuery := 'UPDATE CORE.user_login PARTITION(user_login' || LPAD(partition, 2, '0') || ') SET password_md5 = NULL '
|| 'WHERE password_md5 IS NOT NULL';
EXECUTE IMMEDIATE lQuery;
END LOOP;
END;
Error report:
ORA-06550: line 3, column 5:
PLS-00201: identifier 'LQUERY' must be declared
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored
ORA-06550: line 6, column 23:
PLS-00201: identifier 'LQUERY' must be declared
ORA-06550: line 6, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
when I try to run it. I understand that this might be because of not declaring lQuery but googling online showed me examples of declaring variables of a certain type such as (integer, varchar.. etc.) but none of them showed how to declare a query as this statement expects.
Your statement may be simply declared as a varchar2. For example:
declare
vsql varchar2(1000);
n integer;
begin
vsql := 'select 1 from dual';
execute immediate vsql into n;
dbms_output.put_line(n);
end;

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