Delete the records from table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete the records from table
# 1  
Old 08-21-2013
Delete the records from table

Hi,

Can any one help me...
the records are not deleting when I run the below script.
But if I issue the same delete command manually, the records are getting deleted.

script:
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=mq
Pass=mq
DB=DB1
QMgr=abc
sqlplus ${User}/${Pass}@${DB} << EOF
spool /mw/${QMgr}/log/file.log.`date +"%Y%m%d%H"`;
 
delete from table_name where to_char(CONTRACT_ENDDATE,'yyyy') =(select (to_char(sysdate,'yyyy')- ${1}) from dual);
 
commit;
spool off;
exit
EOF



Result:
Code:
User@ID:/Dir/Qmgr/bin>Script_name.sh 2
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 21 09:38:42 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
 
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> SQL> SQL> SQL> SQL>
0 rows deleted.
SQL> SQL> SQL>
Commit complete.

Here I am trying to delete the records based on the Year.


Moderator's Comments:
Mod Comment Please use code tags for your code and data, thank you

Last edited by zxcjggu708; 08-21-2013 at 12:32 PM.. Reason: code tags and rm formatting
# 2  
Old 08-23-2013
You want to do integer arithmetic? Convert the string years to integers first. Since sysdate is not on the row, get that year in string, convert it to int, take one off, convert back to string and then scan the table. Faster: convert it to a date (yyyy-1) + "-12-31 23:59:59.999" so each row just gets a compare <=, not a convert, Use this year and just make + "-01-01 00:00:00.000" to date with a < compare.

If you really need a range, make a low date the same way. Beware using between, it includes end points.

Last edited by DGPickett; 08-23-2013 at 04:03 PM..
# 3  
Old 08-24-2013
Quote:
Originally Posted by zxcjggu708
...
Code:
#!/bin/ksh
... 
delete from table_name where to_char(CONTRACT_ENDDATE,'yyyy') =(select (to_char(sysdate,'yyyy')- ${1}) from dual);
 ...



Result:
Code:
User@ID:/Dir/Qmgr/bin>Script_name.sh 2
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 21 09:38:42 2013
Copyright (c) 1982, 2005, Oracle. All rights reserved.
...
SQL> SQL> SQL> SQL> SQL> SQL>
0 rows deleted.
SQL> SQL> SQL>
Commit complete.

...
Since you are passing "2" as the argument to your script, the value of ${1} is 2, so your subquery in the "delete" statement returns 2011, like so -

Code:
SQL>
SQL> select to_char(sysdate,'yyyy') - 2 as x from dual;

         X
----------
      2011

1 row selected.

SQL>
SQL>

And so your "delete" statement tries to delete all records that have a CONTRACT_ENDDATE values that lie in the year 2011, like so -

Code:
delete from table_name where to_char(CONTRACT_ENDDATE,'yyyy') = 2011;

But your table does not have any such records, since you see "0 rows deleted" in the log file.

That's what is happening in the script.
Is that what you wanted to do?
# 4  
Old 08-24-2013
Lightbulb Delete the records from table

Thank you very much for your response.
Yes, that is what I wanted to do, and my table contains such records which have year as 2011.
Now I found the solution Smilie, actually I inserted the records with date pattern as '23-JUN-11' and in the below command

Code:
delete from MWDEVUSR.PIERBRIDGE_TENANT_XREF where to_char(CONTRACT_ENDDATE,'yyyy') <= (select (to_char(sysdate,'yyyy')- ${1}) from dual);

that is why it is not deleting the records from the tableSmilie

I changed the command to below,and now it is working fine.

Code:
delete from MWDEVUSR.PIERBRIDGE_TENANT_XREF where to_char(CONTRACT_ENDDATE,'yy') <= (select (to_char(sysdate,'yy')- ${1}) from dual);

Thanks once again.

Last edited by Scrutinizer; 08-24-2013 at 06:56 AM.. Reason: code tags
# 5  
Old 08-28-2013
It lets you subtract int in char, for shame! It must be converting to int gratuitously.

Still very slow and index-unfriendly compared to:
Code:
delete from MWDEVUSR.PIERBRIDGE_TENANT_XREF
 where CONTRACT_ENDDATE
  <= convert( datetime,
         convert( char(4),
              convert( int,
                     to_char( sysdate, 'yy' )
                ) - ${1}
           ) || '-12-31 23:59:59.999999'
        )

One conversion before query, none during.
# 6  
Old 09-24-2013
Code:
delete from MWDEVUSR.PIERBRIDGE_TENANT_XREF where to_char(CONTRACT_ENDDATE,'yy') <= (select (to_char(sysdate,'yy')- ${1}) from dual);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

1. This will insert the records into db table by reading from ta csv file

I have this code with me but the condition is If any of the mandatory columns are null then entire file will be rejected. LOAD DATA infile ' ' #specifies the name of a datafile containing data that you want to load BADFILE ' ' #specifies the name of... (1 Reply)
Discussion started by: raka123
1 Replies

2. Shell Programming and Scripting

Intelligent Script to Insert Records in Database Table

Hello All Members, I am new to this forum and to the shell scripting. I want to write a shell script to do the following: Scenario: I have a pipe delimited .txt file with multiple fields in it. The very first row of the file contains the name of the column which resembles the name of the... (18 Replies)
Discussion started by: chetanojha
18 Replies

3. Shell Programming and Scripting

UNIX Script required for count the records in table

Hi Friends, I looking for the script for the count of the records in table. and then it's containg the zero records then should get abort. and should notify us through mail. Can you please help me out in this area i am lacking. (5 Replies)
Discussion started by: victory
5 Replies

4. Shell Programming and Scripting

Find highest records in table

Dear All, I have table files with different measurements for the same sensors. The first column indicate the sensor (7 different sensors and 16 measurements in the example below). I would like to find the best measurement for each sensor. The best measurement is determined by the higher value of... (10 Replies)
Discussion started by: GDC
10 Replies

5. Shell Programming and Scripting

Getting number of records from a table

I am doing a loading process. I am loading data from a Oracle source to Oracle target. For example there is an SQL statement: Insert into emp_1 Select * from emp_2 where deptno=20; In this case my source is emp_2 and loading into my target table emp_1. This process is automated. Now I... (3 Replies)
Discussion started by: karthikkasarla
3 Replies

6. Shell Programming and Scripting

Total records in table

Hi, I am having two tables. A & B I want to know the total number of records in each table and need to store each value in some variables to process further in the code. How it can be done ? With Regards (2 Replies)
Discussion started by: milink
2 Replies

7. Shell Programming and Scripting

Verifying table records

Hi, Script copies records of two tables into another tables for backup before using oracle's import utility to restore from backup. Now, suppose if the import utility fails then the script will again copy those records from the backup table to the original ones. How to check whether all... (2 Replies)
Discussion started by: milink
2 Replies

8. Shell Programming and Scripting

How do I load records from table to a flat file!

Hi All, I need to load records from oracle table XYZ to a flat file say ABC.dat. could any one tell me how do i do this in UNXI, Regards Ann (1 Reply)
Discussion started by: Haque123
1 Replies

9. UNIX for Advanced & Expert Users

unix script for update or insert records from a file to a oracle table

Hi, I have delimited file(|). Sample data: 1|name|50009|DS24|0|12 2|name|30009|DS24|0|13 3|name|20409|DS24|0|14 4|name|20009|DS24|0|15 5|name|10009|DS24|0|16 I want to load this data into a oracle table (update and insert) Please help me the commands and also... (1 Reply)
Discussion started by: unihp1
1 Replies

10. Shell Programming and Scripting

Inserting records from flat file to db table

I have 20000 numbers present in a file in each line like 25663, 65465, 74579, 56446, .. .. I have created a table in db with single number column in it. create table testhari (no number(9)); I want to insert all these numbers into that table. how can i do it? can anybody please... (4 Replies)
Discussion started by: Hara
4 Replies
Login or Register to Ask a Question