質問

I want to write a sql plus error for when the oracle find a record, or more records, that already exist and just ignore it/them. This is a example:

sqlError=`egrep "ORA-[0-9][0-9][0-9][0-9][0-9]" ${FILE_SPOOL_DAT} | awk '{print $0}';`   
if test ! -f ${FILE_SPOOL_DAT}
then
   echo "Error request " >> ${FILE_SPOOL_DAT}
else
   if [ ! "$sqlError" = "" ] #controls if the variable $sqlError contains a value different from spaces, i think this is the point to change
   then
      echo "Error $sqlError" >> ${FILE_SPOOL_DAT} 
   fi
fi

In this example sqlplus controls if the variable $sqlError contains a value different from spaces. How can i change this condition put the DUPKEY error? Thanks

役に立ちましたか?

解決

If you're using 11g, the IGNORE_ROW_ON_DUPKEY_INDEX hint can help.

SQL> create table table1(a number primary key);

Table created.

SQL> insert into table1 values(1);

1 row created.

SQL> insert into table1 values(1);
insert into table1 values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (JHELLER.SYS_C00810741) violated


SQL> insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(table1(a))*/ into table1 values(1);

0 rows created.

他のヒント

Yikes, that's dangerous! If Oracle finds one or more records that already exists, it will normally rollback the transaction. Suppressing the relevant error messages is way to late.

A much better place is to instruct Oracle to ignore duplicate records directly during the INSERT, for instance by using the MERGE command:

http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9016.htm#i2081218

When you get a duplicate key error it really means you have violated a constraint set on a table. Assuming whoever designed the table understood the data model, what you want to do is a BAD idea. If it is a bad design, change the table's metadata by removing the constraint.

The MERGE command may let you work around it, which I doubt, but with the subsequent data mess left behind I do not believe it is worth the risk.

If you are just playing, remove the constraint anyway. I don't know how your table is set up, but you will probably have to ALTER or DROP and CREATE an index, or ALTER the table.

If this is development for production and thus paid work, don't try to get around the constraint without talking to the person(s) who designed the table to start with.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top