Capture rows for a column in file from delete sql -Oracle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture rows for a column in file from delete sql -Oracle
# 1  
Old 09-07-2012
Capture rows for a column in file from delete sql -Oracle

Hi,
This may not be the right forum but i am hoping someone knows an answer to this.

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 text file that was affected by the above file should have
1234
1235
1236
1237
1238

Thanks - jak
# 2  
Old 09-07-2012
I don't have in depth knowledge of SQL.

But this is sure that you don't need select query.

Try something this..

Code:
DELETE FROM table_name
WHERE some_column=some_value   # here put your condition in where clause currentdatetimestamp > columnDate

Basic SQL
# 3  
Old 09-07-2012
Thanks pamu,
I have the delete sql. I want to capture the rows of a column in a text file that got deleted via the delete sql.

Thanks - jak
# 4  
Old 09-07-2012
Quote:
Originally Posted by jakSun8
...
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 text file that was affected by the above ...
You can use the "returning" clause for this problem.

Code:
SQL>
SQL>
SQL> --
SQL> -- Check the data in the dummy table
SQL> --
SQL> select * from t;
        X Y Z
---------- - ---------
        1 A 01-JAN-12
        2 B 01-FEB-12
        3 C 01-MAR-12
        4 D 01-APR-12
 
4 rows selected.
 
SQL>
SQL> --
SQL> -- My DELETE statement is as follows:
SQL> --
SQL> -- #####################################################
SQL> -- delete
SQL> --   from t
SQL> --  where z >= to_date ('02/01/2012', 'mm/dd/yyyy');
SQL> -- #####################################################
SQL> --
SQL> -- So I know that IDs 2, 3 and 4 will be deleted, and
SQL> -- hence those are the ones that should be returned.
SQL> --
SQL>
SQL> --
SQL> -- Now create a nested table to hold those IDs
SQL> --
SQL> create type tbl_num as table of number;
 2  /
 
Type created.
 
SQL>
SQL> --
SQL> -- Use the "RETURNING" clause of Oracle's DELETE
SQL> -- statement to return the IDs that were deleted.
SQL> -- Then loop and print
SQL> --
SQL> declare
 2    tbl_x  tbl_num;
 3  begin
 4    --
 5    delete
 6      from t
 7     where z >= to_date ('02/01/2012', 'mm/dd/yyyy')
 8    returning x
 9    bulk collect into tbl_x;
10    --
11    for i in tbl_x.first .. tbl_x.last
12    loop
13      dbms_output.put_line ('Deleted Id = ' || tbl_x(i));
14    end loop;
15  end;
16  /
Deleted Id = 2
Deleted Id = 3
Deleted Id = 4
 
PL/SQL procedure successfully completed.
 
SQL>
SQL>
SQL>

I'll leave the creation of a wrapper shell script that prints the IDs to a file as an exercise for you.

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 09-08-2012
Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

2. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

3. UNIX for Dummies Questions & Answers

Delete rows with unique value for specific column

Hi all I have a file which looks like this 1234|1|Jon|some text|some text 1234|2|Jon|some text|some text 3453|5|Jon|some text|some text 6533|2|Kate|some text|some text 4567|3|Chris|some text|some text 4567|4|Maggie|some text|some text 8764|6|Maggie|some text|some text My third column is my... (9 Replies)
Discussion started by: A-V
9 Replies

4. UNIX for Dummies Questions & Answers

how to capture no. of rows updated in update sql in unix db2

hi, i am a new user in unix..and we have unix db2. i want to capture the no. of rows updated by a update db2 sql statement and redirect into a log file. I've seen db2 -m...but not sure how the syntax should be. The update sql that I'm going to run is from a file... Can you please share... (1 Reply)
Discussion started by: j_rymbei
1 Replies

5. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

6. Shell Programming and Scripting

how to delete duplicate rows based on last column

hii i have a huge amt of data stored in a file.Here in this file i need to remove duplicates rows in such a way that the last column has different data & i must check for greatest among last colmn data & print the largest data along with other entries but just one of other duplicate entries is... (16 Replies)
Discussion started by: reva
16 Replies

7. Shell Programming and Scripting

How to capture value in shell variable from oracle sql?

Hi Friends, Do someone know how to capture value in a shell variable from oracle sql? Requirement : In a table we want to count the number of records and want to pass this value to a shell variable where it can be manipulated later. In ksh shell we open oracle connection from sqlplus. For... (1 Reply)
Discussion started by: sourabhsharma
1 Replies

8. Shell Programming and Scripting

How to delete particular rows from a file

Hi I have a file having 1000 rows. Now I would like to remove 10 rows from it. Plz give me the script. Eg: input file like 4 1 4500.0 1 5 1 1.0 30 6 1 1.0 4500 7 1 4.0 730 7 2 500000.0 730 8 1 785460.0 45 8 7 94255.0 30 9 1 31800.0 30 9 4 36000.0 30 10 1 15000.0 30... (5 Replies)
Discussion started by: suresh3566
5 Replies

9. UNIX for Dummies Questions & Answers

Running file sql from shell without capture the password

Dear All I have file a.sql, let's say the content is: _________________________________ select * from dual; exit; _________________________________ and I have shell script a.sh, the content is: _________________________________ ORACLE_SID=testing; export ORACLE_SID... (0 Replies)
Discussion started by: Aditya Purwanto
0 Replies

10. Shell Programming and Scripting

flags to suppress column output, # of rows selected in db2 sql in UNIX

Hello, I am new to db2 SQL in unix so bear with me while I try to explain the situation. I have a text file that has the contents of the where condition that I am using for a db2 SQL in UNIX ksh. Here is the snippet. if ; then echo "Begin processing VALUEs" ... (1 Reply)
Discussion started by: jerardfjay
1 Replies
Login or Register to Ask a Question