Deleting the records based on the condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting the records based on the condition
# 1  
Old 07-30-2013
Computer Deleting the records based on the condition

Hi,

Can any one help me, in deleting the records from the database table based on the following condition:
script should take a configurable parameter as input. The input is nothing but “no. of years”.
For example, if I enter 2 as input parameter, then the 2 year old records should get deleted. In the same way if i enter 3 as a parameter then 3 years old records should be deleted and so on...


its a great help,

thanks,
zxcjggu.
# 2  
Old 07-30-2013
Much more info needed...

What kind of database?
What is the record layout?
What field is being matched against your input parameter?
and on and on....
# 3  
Old 07-30-2013
Hi joeyg,
Please find the below details::

Testpurging.sh 2
Response::

Code:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> SQL> DELETE FROM Testpurging TAI WHERE TAI.YEAR in ( select TAI1.YEAR from Testpurging MAX(ENTRY_DATE) < (select add_months(SYSDATE,-2) from dual))
                                                                                     *
ERROR at line 1:
ORA-00907: missing right parenthesis


SQL>
Commit complete.

Request

Code:
#!/bin/ksh
USAGE_STRING="USAGE $0 [Purge_criteria]"
if [ "`whoami`" != "mqm" ]
        then
        echo "SORRY you need to be user 'mqm'. Only 'mqm' has required permissions."
        return 1
fi
##
if [ -z "${1}" ]
        then
        echo "No purge criteria was specified. Purge Criteria is the number-of-months."
        echo "${USAGE_STRING}"
        return 1
fi
export ORACLE_HOME=/u001/oracle/product/10.2.0
export ORACLE_BASE=/u001/oracle
export ORACLE_DOC=/u001/oracle/doc
export ORACLE_TERM=vt100
export LD_LIBRARY_PATH=/u001/oracle/product/10.2.0/lib32:/usr/dt/lib
export PATH=${PATH}:${ORACLE_HOME}/bin
User=mqiuedev01
Pass=mqiuedev01
DB=mwdbd1
QMgr=PBD1MWD1
sqlplus ${User}/${Pass}@${DB} << EOF
spool /mw/${QMgr}/log/Testpurging.log.`date +"%Y%m%d%H%M%S"`;
DELETE FROM Testpurging TAI WHERE TAI.YEAR in ( select TAI1.YEAR from Testpurging MAX(ENTRY_DATE) < (select add_months(SYSDATE,-${1}) from dual)) ;
commit;
spool off;
exit
EOF


Last edited by Franklin52; 07-30-2013 at 09:59 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting the records once condition met

Hi All, Seeking for your assistance to get the records once the $2 met the condition. Ex. file 1.txt 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 123232,11-Aug-2015 08:33:37 PM,4234324,1321432,34364 Output: 123455,10-Aug-2020 07:33:37 AM,2335235,1323534,12343 What i did... (5 Replies)
Discussion started by: znesotomayor
5 Replies

2. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

3. Shell Programming and Scripting

Deleting lines based on a condition for a group of files

hi i have a set of similar files. i want to delete lines until certain pattern appears in those files. for a single file the following command can be used but i want to do it for all the files at a time since the number is in thousands. awk '/PATTERN/{i++}i' file (6 Replies)
Discussion started by: anurupa777
6 Replies

4. UNIX for Dummies Questions & Answers

Deleting the unwanted data based on condition

hi i have my input data like this aaa bbb ccc asa dff nmj mnj saa dff oik aax cdx saa oik asq sdf dssi want my output file to be like this mnj saa dff oik aax cdx saa oiki want to retain only those lines which will have oik just below them and i want oik to be as next column to those... (1 Reply)
Discussion started by: anurupa777
1 Replies

5. Shell Programming and Scripting

Counting the records based on condition

Hi Unix team, I have a file with 30 columns with tab delimited. I need to count the records based on column 18 & 21 data. So I cut the data from the file using awk -F"\t" '{print $18,$21}' foo.txt Following is the output: USED SEDAN USED SUV NEW SUV USED Truck USED Truck USED... (6 Replies)
Discussion started by: karumudi7
6 Replies

6. UNIX for Dummies Questions & Answers

Delete records from a big file based on some condition

Hi, To load a big file in a table,I have a make sure that all rows in the file has same number of the columns . So in my file if I am getting any rows which have columns not equal to 6 , I need to delete it . Delimiter is space and columns are optionally enclosed by "". This can be ... (1 Reply)
Discussion started by: hemantraijain
1 Replies

7. Shell Programming and Scripting

Print other records along with condition too

Hi, I have a text file with more than 100000 records. I used the following command awk '{if ($3<$2) print $1"\t"$3"\t"$2"\t"$4"\t"$5}' input.txt > output.txt But, this one is printing which satisfies the condition, which are only 1000. I would like to get all the 100000 records in... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

8. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

9. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

10. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies
Login or Register to Ask a Question