i have successfully implemented a proc which can delete delete duplicate records from a table .
Carrying on i am further trying to generalize this procedure using Dynamic sql But i get the following error when compiling itself .
ERROR line 13, col 7, ending_line 14, ending_col 14, Found ''SELECT ROWID, ERR_MOD
FROM '', Expecting: ( SELECT -or- WITH
Here is my code :-
CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
p_tblname IN VARCHAR2,
p_cname IN varchar2,
p_cvalue IN varchar2,
p_out OUT VARCHAR2:=0
)
IS
v_cnt NUMBER;
CURSOR TMP
IS
'SELECT ROWID, ERR_MOD
FROM ' || p_tblname||
'WHERE '|| p_cname '='||''p_cvalue '';
BEGIN
BEGIN
'SELECT COUNT ( * )
INTO v_cnt
FROM ' || p_tblname||
'WHERE '|| p_cname' = '||''p_cvalue'';
EXCEPTION
WHEN OTHERS
THEN
p_out:=1;
end;
FOR REC IN tmp
LOOP
IF v_cnt = 1
THEN
EXIT;
ELSE
'DELETE FROM '|| p_tblname||
'WHERE ROWID = REC.ROWID';
END IF;
v_cnt := v_cnt - 1;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
p_out := 2;
END;
/
Hi you cannot create a dynamic query for normal cursors in such cases you should use ref cursors and follow the the steps below
--create a query string
--open the ref cursor
and please use execute immediate for execution of delete statements...
CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
p_tblname IN VARCHAR2,
p_cname IN varchar2,
p_cvalue IN varchar2,
p_out OUT VARCHAR2:=0
IS
v_cnt NUMBER;
v_string varchar2(200);
TYPE ref_cur IS REF CURSOR
ref_cursor_name ref_cur;
BEGIN
v_string:='SELECT ROWID, ERR_MOD FROM ' || p_tblname|| 'WHERE '|| p_cname
'='||''p_cvalue '';
OPEN cursor_variable_name FOR v_string;
.....
.....
EXECUTE IMMEDIATE 'DELETE FROM '|| p_tblname|| 'WHERE ROWID = REC.ROWID';--you need execute immediate for this
.....
.....
Related
I'm currently learning PL/SQL. I need to create a PL/SQL block to create a backup of all my tables like this : myTable -> myTable_old.
Here's what I got right now :
DECLARE
Cursor c IS SELECT table_name
FROM user_tables
WHERE table_name NOT LIKE '%_old';
sql_slc VARCHAR2(200);
sql_drp VARCHAR2(200);
sql_crt VARCHAR2(200);
row_count NUMBER;
t_name user_tables.table_name%type;
t_backup_name user_tables.table_name%type;
BEGIN
sql_drp := 'DROP TABLE :1 CASCADE';
sql_crt := 'CREATE TABLE :1 AS SELECT * FROM :2';
sql_slc := 'SELECT COUNT(*) FROM user_tables WHERE table_name = :1';
OPEN c;
LOOP
FETCH c INTO t_name;
EXIT WHEN (c%NOTFOUND);
t_backup_name := t_name || '_old';
dbms_output.put_line(t_name || ' ' || t_backup_name);
EXECUTE IMMEDIATE sql_slc INTO row_count USING t_backup_name;
IF row_count > 0 THEN
dbms_output.put_line(t_backup_name || ' dropped');
EXECUTE IMMEDIATE sql_drp USING t_backup_name;
END IF;
dbms_output.put_line(t_backup_name || ' created');
EXECUTE IMMEDIATE sql_crt USING t_backup_name, t_name;
COMMIT;
END LOOP;
CLOSE c;
END;
/
Here's the error :
OUVRAGE OUVRAGE_old
OUVRAGE_old created
DECLARE
*
ERROR on line 1 :
ORA-00903: table name not valid
ORA-06512: on line 29
I don't understand why this error is coming up, can someone help me ?
The issue is that you can not use bind variables for table names; Oracle documentation:
The database uses the values of bind variables exclusively and does
not interpret their contents in any way.
You should edit your code to use concatenation instead:
DECLARE
Cursor c IS SELECT table_name
FROM user_tables
WHERE table_name NOT LIKE '%_OLD'; /* OLD, upper case */
sql_slc VARCHAR2(200);
--sql_drp VARCHAR2(200);
--sql_crt VARCHAR2(200);
row_count NUMBER;
t_name user_tables.table_name%type;
t_backup_name user_tables.table_name%type;
BEGIN
-- sql_drp := 'DROP TABLE :1 CASCADE';
-- sql_crt := 'CREATE TABLE :1 AS SELECT * FROM :2';
sql_slc := 'SELECT COUNT(*) FROM user_tables WHERE table_name = :1';
OPEN c;
LOOP
FETCH c INTO t_name;
EXIT WHEN (c%NOTFOUND);
t_backup_name := t_name || '_OLD'; /* OLD, upper case */
DBMS_OUTPUT.put_line (t_name || ' ' || t_backup_name);
EXECUTE IMMEDIATE sql_slc INTO row_count USING t_backup_name;
IF row_count > 0
THEN
DBMS_OUTPUT.put_line (t_backup_name || ' dropped');
-- EXECUTE IMMEDIATE sql_drp USING t_backup_name;
EXECUTE IMMEDIATE ' drop table ' || t_backup_name; /* concatenation and not bind variables */
END IF;
DBMS_OUTPUT.put_line (t_backup_name || ' created'); /* concatenation and not bind variables */
-- EXECUTE IMMEDIATE sql_crt USING t_backup_name, t_name;
EXECUTE IMMEDIATE 'create table ' || t_backup_name || ' as select * from ' || t_name;
COMMIT;
END LOOP;
CLOSE c;
END;
Also, notice that, if not double quoted, object names always are uppercase, so you have to look for t_name || '_OLD' and not t_name || '_old'
i am in the middle of studys and currently doing some exercises.
I am trying to make procedure that shows how many duplicats are in a row.
CREATE OR REPLACE PROCEDURE anzahl1(
tabelle VARCHAR2,
reihe VARCHAR2,
wieviel OUT NUMBER
)AS
test VARCHAR2(4000);
BEGIN
EXECUTE IMMEDIATE 'SELECT
reihe,
(COUNT(*)-1) AS Anzahl
INTO
wieviel
FROM
tabelle
GROUP BY
reihe
HAVING COUNT(*) > 1' using out wieviel;
DBMS_OUTPUT.PUT_LINE(wieviel);
END;
/
i tried many things but nothing worked...
even the result i was given by a teacher of my old school didnt work:
CREATE OR REPLACE PROCEDURE check_doppelte_Werte_p (
p_tabellenname IN USER_TAB_COLUMNS.TABLE_NAME%TYPE,
p_spaltenname IN USER_TAB_COLUMNS.COLUMN_NAME%TYPE,
p_ergebnis BOOLEAN
) OUT
IS
v_dummy NUMBER := 1;
v_sql_anweisung varchar2 (4000);
BEGIN
v_sql_anweisung :=
'SELECT MAX(COUNT ('
|| p_spaltenname
|| ')) '
|| ' FROM '
|| p_tabellenname
|| ' GROUP BY '
|| p_spaltenname;
DBMS_OUTPUT.PUT_LINE (v_sql_anweisung);
EXECUTE IMMEDIATE v_sql_anweisung INTO v_dummy;
IF v_dummy > 1 THEN
DBMS_OUTPUT.PUT_LINE( 'Die Tabelle '
|| 'hat mindestens '
|| TO_CHAR (V_DUMMY)
|| ' doppelte Werte in der Spalte '
|| p_spaltenname);
p_ergebnis := TRUE;
ELSE
p_ergebnis := FALSE;
END IF;
EXCEPTION
-- Keine Werte gefunden, da
WHEN NO_DATA_FOUND
THEN
p_ergebnis := FALSE;
DBMS_OUTPUT.PUT_LINE ('Tabellenname oder Spaltenname sind nicht vorhan-
den!!!');
END;
What would you do/change?
Your code doesn't substitute the variables in the dynamic sql.The query String has been to be appended with the input variables.
Please try the code below,
CREATE OR REPLACE PROCEDURE anzahl1(
tabelle VARCHAR2,
reihe VARCHAR2,
wieviel OUT NUMBER
)AS
test VARCHAR2(4000);
lv_query VARCHAR2(4000);
BEGIN
lv_query := 'SELECT (COUNT(*)-1) cnt FROM '||tabelle ||' GROUP BY '||reihe||' HAVING COUNT(*) > 1';
EXECUTE IMMEDIATE lv_query into wieviel;
DBMS_OUTPUT.PUT_LINE(wieviel);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR '||SQLCODE||' '||SUBSTR(SQLERRM,1,200));
END;
/
I create table and populate records as below,
CREATE TABLE EMP(ID NUMBER(10),NAME VARCHAR2(40));
INSERT INTO EMP VALUES (1, 'TEST');
INSERT INTO EMP VALUES (1, 'TEST');
INSERT INTO EMP VALUES (2, 'TEST2');
Now i call the stored procedure to test for EMP table for ID column,
declare
lv_cnt number(10);
begin
anzahl1('EMP','ID',lv_cnt);
dbms_output.put_line(lv_cnt);
end;
This give output as ,
1
Can we pass partition as parameter in stored procedure ? I'm getting following error.
Please help on this.
CREATE OR REPLACE PROCEDURE PROCEDURE_NAME (PARTITION_NAME IN VARCHAR2)
IS
LASTDATE VARCHAR2(12);
ENDDATE VARCHAR2(12);
BEGIN
IF PARTITION_NAME='DEC_2014' OR PARTITION_NAME='JAN_2015' OR PARTITION_NAME='MAR_2015' OR PARTITION_NAME='MAY_2015' OR PARTITION_NAME='JUL_2015' OR PARTITION_NAME='AUG_2015' OR PARTITION_NAME='OCT_2015' OR PARTITION_NAME='DEC_2015' OR PARTITION_NAME='JAN_2016' THEN
ENDDATE:='31';
ELSIF PARTITION_NAME='NOV_2014' OR PARTITION_NAME='APR_2015' OR PARTITION_NAME='JUN_2015' OR PARTITION_NAME='SEP_2015' OR PARTITION_NAME='NOV_2015' THEN
ENDDATE:='30';
ELSE
ENDDATE:='28';
END IF;
LASTDATE:=CONCAT(CONCAT(ENDDATE,'-'),REPLACE (PARTITION_NAME, '_', '-'));
DBMS_OUTPUT.PUT_LINE(LASTDATE);
DBMS_OUTPUT.PUT_LINE(PARTITION_NAME);
UPDATE
/*+ PARALLEL(Alias 4) */
TABLE_NAME PARTITION (PARTITION_NAME) Alias
SET Alias.Alias_D_EFFECTIVE_DATE =
(SELECT
/*+ PARALLEL(Alias3 4) */
ALAIS2.ALAIS2_D_DATETIME
FROM schema1.TABLE_2 ALAIS2
WHERE TRUNC(ALAIS2_D_DATETIME) <= TO_DATE(LASTDATE,'DD-MON-YYYY')
AND ALAIS2.ALAIS2_N_TRN_ID = Alias.Alias_N_PR
)
WHERE EXISTS
(SELECT Alias3.ALAIS2_D_DATETIME
FROM schema1.TABLE_2 Alias3
WHERE TRUNC(Alias3.ALAIS2_D_DATETIME) <= TO_DATE(LASTDATE,'DD-MON-YYYY')
AND Alias3.ALAIS2_N_TRN_ID = Alias.Alias_N_PR
);
COMMIT;
END PROCEDURE_NAME;
Error:
Error at line 1
ORA-02149: Specified partition does not exist
ORA-06512: at "schema1.PROCEDURE_NAME", line 17
ORA-06512: at line 1
I think you can do by Dynamic SQL Statements, EXECUTE IMMEDIATE Statement
follow this link ...
https://docs.oracle.com/cd/B10501_01/appdev.920/a96590/adg09dyn.htm
https://docs.oracle.com/cloud/latest/db112/LNPLS/dynamic.htm#LNPLS01115
CREATE OR REPLACE PROCEDURE PROCEDURE_NAME (PARTITION_NAME IN VARCHAR2)
IS
LASTDATE VARCHAR2(12);
ENDDATE VARCHAR2(12);
query_str VARCHAR2(1000);
BEGIN
//.......your code.......
query_str := 'UPDATE TABLE_NAME PARTITION (' || PARTITION_NAME || ') Alias '
||'SET Alias.Alias_D_EFFECTIVE_DATE =.....'
EXECUTE IMMEDIATE query_str;
COMMIT;
END PROCEDURE_NAME;
Hope this will work.
Can you tell me what is wrong with this procedure that I'm trying to create?
CREATE OR REPLACE PROCEDURE create_audit_tables (table_owner VARCHAR2)
IS
CURSOR c_tables (
table_owner VARCHAR2)
IS
SELECT ot.owner AS owner, ot.table_name AS table_name
FROM all_tables ot
WHERE ot.owner = table_owner
AND ot.table_name NOT LIKE 'AUDIT_%'
AND ot.table_name <> 'EXAUDIT'
AND NOT EXISTS
(SELECT 1
FROM EXAUDIT efa
WHERE ot.table_name = efa.tname)
AND NOT EXISTS
(SELECT 1
FROM all_tables at
WHERE at.table_name = 'AUDIT_'||ot.table_name);
v_sql VARCHAR2 (8000);
v_count NUMBER := 0;
v_aud VARCHAR2 (30);
BEGIN
FOR r_table IN c_tables (table_owner)
LOOP
BEGIN
v_aud := 'AUDIT_'||r_table.table_name;
v_sql :=
'create table '
|| v_aud
|| ' as select * from '
|| r_table.owner
|| '.'
|| r_table.table_name
|| ' where 1 = 1';
DBMS_OUTPUT.put_line ('Info: ' || v_sql);
EXECUTE IMMEDIATE v_sql;
v_sql :=
'alter table '
|| v_aud
|| ' add ( AUDIT_ACTION char(1), AUDIT_BY varchar2(50), AUDIT_AT TIMESTAMP)';
EXECUTE IMMEDIATE v_sql;
v_count := c_tables%ROWCOUNT;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (
'Failed to create table '
|| v_aud
|| ' due to '
|| SQLERRM);
END;
END LOOP;
IF v_count = 0
THEN
DBMS_OUTPUT.put_line ('No audit tables created');
ELSE
DBMS_OUTPUT.put_line (v_count || ' audit tables created.');
END IF;
END;
/
I checked it with show errors function and it gave me this:
LINE/COL ERROR
-------- --------------------------------------------
6/1 PL/SQL: SQL Statement ignored
16/3 PL/SQL: ORA-00906: missing left parenthesis
Perhaps the problem is quite simple, but I can't see it, so please help me if you can
I can compile procedure without problems after creating table
create table EXAUDIT (tname varchar2(100));
Check status of procedure:
select status from user_objects where lower(object_name) = 'create_audit_tables';
I have a procedure that will select MAX from some tables, but for some reason it is not able to find these tables. Could anybody help me?
declare
varible1 varchar2 (255);
temp varchar2 (255);
last_val number(9,0);
cursor c1 is Select distinct table_name from user_tab_cols order by table_name;
begin
FOR asd in c1
LOOP
temp := asd.table_name;
varible1 := '"'||temp||'"';
select max("id") into last_val from varible1 ;
END LOOP;
end;
For example, the first table name is acceptance_form and for select I need to use "acceptance_form".
Code after edit:
declare
varible1 varchar2 (255);
temp varchar2 (255);
last_val number(9,0);
cursor c1 is Select distinct table_name from user_tab_cols where column_name = 'id';
begin
FOR asd in c1
LOOP
temp := asd.table_name;
execute immediate 'select NVL(max('||'id'||'),0) from "'||varible1||'"' into last_val;
END LOOP;
end;
Can't cuz db is Case sensitive Oracle express 10g tables and rows was created like this
CREATE TABLE "ADMINMME"."acceptance_form"
(
"group_id" NUMBER(9, 0),
"id" NUMBER(4, 0) DEFAULT '0' NOT NULL ,
"is_deleted" NUMBER(4, 0),
"name" NVARCHAR2(30) NOT NULL
);
Can u tell me how to handle exception sequence dosn't exist for this;
Nevermind exception was in wrong block :)
declare
temp varchar2 (255);
last_val number(9,0);
cursor c1 is Select distinct table_name from user_tab_cols where column_name = 'id';
begin
FOR asd in c1
LOOP
temp := asd.table_name;
execute immediate 'select NVL(max("id"),0)+1 from "'||temp||'"' into last_val;
begin
EXECUTE IMMEDIATE 'drop sequence "seq_'|| temp||'"';
EXECUTE IMMEDIATE 'create SEQUENCE "seq_'|| temp ||'" MINVALUE '||last_val||'MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCACHE';
EXECUTE IMMEDIATE 'select '||temp||'.nextval from dual';
EXECUTE IMMEDIATE 'ALTER SEQUENCE "seq_'||temp||'" INCREMENT BY 1';
exception when others then
null;
end;
END LOOP;
end;
Dynamic sql doesn't work in that way.
declare
varible1 varchar2 (255);
temp varchar2 (255);
last_val number(9,0);
cursor c1 is Select distinct table_name from user_tab_cols order by table_name;
begin
FOR asd in c1
LOOP
temp := asd.table_name;
begin
execute immediate 'select max(id) from '||temp into last_val;
dbms_output.put_line('max(id) for table: ' ||temp||' = '||last_val);
exception when others then
dbms_output.put_line('Failed to get max(id) for table: ' ||temp);
end;
END LOOP;
end;
You can't use a variable for the table name.
What you can do is creating the complete sql statement as a string and use execute immediate
Here are some examples how to do that: http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/dynamic.htm#CHDGJEGD