0

I have to capture rows for a column that was deleted. How can i do that without having to write a select query?

delete from myschema.mytable where currentdatetimestamp > columnDate 

this should delete 5 rows for example

I want to capture rows of columnID (not all the columns of the table) in a file that was affected by the above file should have

1234
1235
1236
1237

1238

4
  • What kind of "file"? Another database? A table in the same database? A text file? Commented Sep 8, 2012 at 1:15
  • 1
    "...capture rows for a column that was deleted", I'm afraid you can't. Rows are already deleted right? Commented Sep 8, 2012 at 1:25
  • @KenWhite - yes write the data to a text file before it gets deleted. Commented Sep 8, 2012 at 1:29
  • The only way to do this would be in a DELETE trigger, as @BobJarvis says below - it is called as the rows are being deleted though, not after. Once they're deleted, it's too late. What are you actually trying to accomplish, though? There's probably a better way to do it than with a text file. Commented Sep 8, 2012 at 1:34

2 Answers 2

1

You can use the RETURNING clause of the DELETE statement (Adapted this example in the Oracle Docs):

DECLARE
   TYPE NumList IS TABLE OF myschema.mytable.columnID%TYPE;
   IDs NumList;
BEGIN
   DELETE FROM myschema.mytable WHERE currentdatetimestamp > columnDate
      RETURNING columnID BULK COLLECT INTO IDs;
   DBMS_OUTPUT.PUT_LINE('Deleted ' || SQL%ROWCOUNT || ' rows:');
   FOR i IN IDs.FIRST .. IDs.LAST
   LOOP
      DBMS_OUTPUT.PUT_LINE('columnID: '||IDs(i));
   END LOOP;
END;
/

You can do whatever you like with the resulting collection of IDs.

Sign up to request clarification or add additional context in comments.

Comments

0

It may be possible to do what you want by using a DELETE trigger. Following is an example:

CREATE OR REPLACE TRIGGER MY_TABLE_DELETE_TRIGGER
  BEFORE DELETE ON MY_TABLE
  FOR EACH ROW
BEGIN
  DBMS_OUTPUT.PUT_LINE('Deleted row with ID=' || :OLD.COLUMN_ID);
END MY_TABLE_DELETE_TRIGGER;

This is just an example - you should modify it to do whatever you need.

Share and enjoy.

4 Comments

I agree that would be the only possible way to do it. I'm not sure of what the purpose would be, though; since the poster didn't explain what the reason for doing so is, it's hard to say whether this is the right answer or not though.
Thanks - i want to capture those IDs because there are corresponding files on the file system that needs to be deleted as well. So i will loop through file and delete 1234.xml, 1235.xml, etc..
@Jay - perhaps you could use UTL_FILE.FREMOVE()? The problem here is that if your transaction get rolled back for some reason, the file would be removed but the row would still remain.
@BobJarvis - i am not familiar with that. Let me read on it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.