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:

#!/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:

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.

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.

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 -

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 -

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?

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 :b:, actually I inserted the records with date pattern as '23-JUN-11' and in the below command

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 table:)

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

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

Thanks once again.

It lets you subtract int in char, for shame! It must be converting to int gratuitously.

Still very slow and index-unfriendly compared to:

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.

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