Solved

DECLARE and DELETE in Oracle

Posted on 2011-03-03
10
460 Views
Last Modified: 2012-05-11
I have the following SQL in oracle which is failing - can someone please tell what I'm doing wrong?

DECLARE ACCT VARCHAR(20 CHAR) := "00000000000011434562";
BEGIN
    DELETE FROM deb.ACCOUNT           WHERE CUSTOMERNO = ACCT;
    DELETE FROM deb.CUSTOMER          WHERE CUSTOMERNO = ACCT;
    DELETE FROM deb.OTHERTABLE      WHERE CUSTOMERNO = ACCT;
    COMMIT;
END;

Note:  I am using SQuerreL as my interface.  --The following SQL works but I don't want to edit all the strings because in reality there are 20 tables to update.

    DELETE FROM deb.ACCOUNT           WHERE CUSTOMERNO = "00000000000011434562";
    DELETE FROM deb.CUSTOMER          WHERE CUSTOMERNO = "00000000000011434562";
    DELETE FROM deb.OTHERTABLE      WHERE CUSTOMERNO = "00000000000011434562";
    COMMIT;


Any help would be greatly appreciated.

Mike
0
Comment
Question by:MikeDelaney
10 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
put your strings in single quotes,  not double
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
try this :

DECLARE
ACCT VARCHAR2(20 CHAR) := "00000000000011434562";
BEGIN
    DELETE FROM deb.ACCOUNT           WHERE CUSTOMERNO = ACCT;
    DELETE FROM deb.CUSTOMER          WHERE CUSTOMERNO = ACCT;
    DELETE FROM deb.OTHERTABLE      WHERE CUSTOMERNO = ACCT;
    COMMIT;
END;
/

also ensure there is no column with name ACCT in your tables otherwise it will not work. to be on the safer side, just have another variable name instead of ACCT.

so try this :

DECLARE
my_ACCT_var VARCHAR2(20 CHAR) := "00000000000011434562";
BEGIN
    DELETE FROM deb.ACCOUNT           WHERE CUSTOMERNO = my_ACCT_var;
    DELETE FROM deb.CUSTOMER          WHERE CUSTOMERNO = my_ACCT_var;
    DELETE FROM deb.OTHERTABLE      WHERE CUSTOMERNO = my_ACCT_var;
    COMMIT;
END;
/
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
Comment Utility
missed to change quotes, so try this :

DECLARE
my_ACCT_var VARCHAR2(20 CHAR) := '00000000000011434562';
BEGIN
    DELETE FROM deb.ACCOUNT           WHERE CUSTOMERNO = my_ACCT_var;
    DELETE FROM deb.CUSTOMER          WHERE CUSTOMERNO = my_ACCT_var;
    DELETE FROM deb.OTHERTABLE      WHERE CUSTOMERNO = my_ACCT_var;
    COMMIT;
END;
/
0
 
LVL 2

Expert Comment

by:choukssa
Comment Utility
DECLARE

s_account VARCHAR2(20) := '00000000000011434562';
s_query   VARCHAR2(1000);

Try this :

BEGIN

    FOR rec IN  
    (
      select owner, table_name from all_tab_columns
      where owner = 'DEB' and COLUMN_NAME = 'CUSTOMERNO'                                  
    )
    LOOP    

        s_query:= 'DELETE FROM '|| rec.owner ||'.' || rec.table_name || ' WHERE CUSTOMERNO = ' || s_account;  
        execute immediate s_query;        

    END LOOP;
    COMMIT;
END;
/

Open in new window

--choukssa
0
 
LVL 2

Expert Comment

by:choukssa
Comment Utility
Sorry I messed up the last post


Try this
DECLARE

s_account VARCHAR2(20) := '00000000000011434562';
s_query   VARCHAR2(1000);

BEGIN

    FOR rec IN  
    (
      select owner, table_name from all_tab_columns
      where owner = 'DEB' and COLUMN_NAME = 'CUSTOMERNO'                                  
    )
    LOOP    

        s_query:= 'DELETE FROM '|| rec.owner ||'.' || rec.table_name || ' WHERE CUSTOMERNO = ' || s_account;  
        execute immediate s_query;        

    END LOOP;
    COMMIT;
END;
/

Open in new window

--choukssa
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Expert Comment

by:choukssa
Comment Utility
Made it generic and reduced potential extra updates.
DECLARE
    s_account VARCHAR2(20) := '00000000000011434562';
    s_query   VARCHAR2(1000);
    s_column VARCHAR2(50) := 'CUSTOMERNO';
    s_owner  VARCHAR2(50) := 'DEB';
BEGIN
    FOR rec IN  
    (
      select distinct table_name from all_tab_columns where owner = s_owner and COLUMN_NAME = s_column                                  
    )
    LOOP    
        s_query:= 'DELETE FROM '|| s_owner ||'.' || rec.table_name || ' WHERE ' || s_column || ' = ' || s_account;  
        execute immediate s_query;        
    END LOOP;
    COMMIT;
END;
/

Open in new window


--choukssa
0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
looks ok to me.

But just a note here, if there are two tables ( one parent and one child which has foreign key relationship ), then your delete has to delete from the child first. If you try to delete from the parent when the child records exist, then you will get error. Not sure whether you have ON DELETE CASCADE for all your parent/child tables which can take care of deleting child records when you delete from the parent table.

Thanks
0
 
LVL 2

Expert Comment

by:choukssa
Comment Utility
In that case we can run a loop to disable all referential constraints that have that column before the delete script and enable them afterwards.

--choukssa
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>disable all referential constraints

I would not recommend disabling constraints on a live database unless there is absolutely no other way.

Doing so can allow dirty data to enter the system.  This is even more likely if applications have been coded to trap integrity errors and take appropriate action.
0
 

Author Closing Comment

by:MikeDelaney
Comment Utility
Sorry for the delay but this worked out great - Thanks so much.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now