Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Nikhil.

Asked: June 17, 2016 - 11:56 am UTC

Last updated: June 18, 2016 - 6:08 am UTC

Version: 11G

Viewed 10K+ times! This question is

You Asked

1. if a plsql block contains insert and update query and one of the query failed how to identify which query failed ?
2. what are collection types in oracle ??


and Connor said...

Well... and insert-values will give an error if it fails, so its pretty obvious:

SQL> create table t (x int check (x > 0 ) );

Table created.

SQL>
SQL> insert into t values (1);

1 row created.

SQL>
SQL> begin
  2    insert into t values (0);
  3  end;
  4  /
begin
*
ERROR at line 1:
ORA-02290: check constraint (MCDONAC.SYS_C0020561) violated
ORA-06512: at line 2



Perhaps you mean insert-select ? or if an update does not find any rows ?, eg

SQL> begin
  2    update t set x = 2
  3    where  sysdate > date '2020-01-01';
  4
  5    insert into t
  6    select 2
  7    from   dual
  8    where  sysdate > date '2020-01-01';
  9  end;
 10  /

PL/SQL procedure successfully completed.


In this case, both commands did nothing...so how do we know?

We can use the %rowcount attribute

SQL> begin
  2    update t set x = 2
  3    where  sysdate > date '2020-01-01';
  4
  5    if sql%rowcount = 0 then
  6       raise_application_error(-20000,'My update did not touch any rows');
  7    end if;
  8
  9    insert into t
 10    select 2
 11    from   dual
 12    where  sysdate > date '2020-01-01';
 13
 14    if sql%rowcount = 0 then
 15       raise_application_error(-20000,'My insert did not create any rows');
 16    end if;
 17
 18  end;
 19  /
begin
*
ERROR at line 1:
ORA-20000: My update did not touch any rows
ORA-06512: at line 6


Collection types are just similar to many languages where you can store more complex data structures in a single variable or object. Collection types are similar to arrays in that respect.

Plenty of good info in the docs

http://docs.oracle.com/database/121/LNPLS/composites.htm


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library