?
Solved

DECLARE and DELETE in Oracle

Posted on 2011-03-03
10
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 35029663
put your strings in single quotes,  not double
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 35032812
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 2000 total points
ID: 35032825
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 2

Expert Comment

by:choukssa
ID: 35041376
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
ID: 35041382
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
 
LVL 2

Expert Comment

by:choukssa
ID: 35041392
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
ID: 35054049
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
ID: 35056472
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 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35056483
>>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
ID: 35335511
Sorry for the delay but this worked out great - Thanks so much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

650 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