Sunday, February 3, 2013

Limitations in calling a Stored Function

To invoke/call a function from SQL statements/subprograms it should obey :

1. When called from a SELECT statement,function cannot modify any tables.

2. When invoked from a SELECT,INSERT,UPDATE or DELETE constructs,
function cannot query/modify any tables modified by that statement.

3. When invoked from SELECT,INSERT,UPDATE or DELETE statements, the function cannot
execute transaction control statements/DDL's.


To check point # 1, lets try do what is mentioned :

1. Create a table 'xx_func_dml'

create table xx_func_dml
(
id number(4),
name varchar2(20)
)

begin
insert into xx_func_dml values (1,'NAG');
insert into xx_func_dml values (2,'SRI');
commit;
end;
/


2.create a function xx_func_call, which updates a table xx_func_dml.
create or replace function xx_func_call
return number
is
begin
dbms_output.put_line('In func to execute a dml statement on
                      table. Also func is called in select query');
update xx_func_dml
set name='TOM'
where id=2;
commit;
return 1;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
/


Call function normally and see

declare
i number;
begin
i:=xx_func_call;
dbms_output.put_line(i);
commit;
end;
/

Now call function from a select query and check :

Select xx_func_call from dual;