We help IT Professionals succeed at work.

Using Custom Functions within a view. Possible?

gram77
gram77 asked
on
539 Views
Last Modified: 2013-12-19
I have been told that custom functions can't be created in a view.

But I have been able to create and use costom functions in a view.

Here is an example.

Please Verify?


create table test_tbl
(name varchar2(10),
 date1 date);

insert into test_tbl values
('abc','01-mar-2007',
 'def','01-apr-2007');

commit;

create or replace function test_func(p_date varchar2)
return varchar2
is
begin
      return to_char(to_date(sysdate),'DDD-MM-YY');
end;
/
show errors

create or replace view test_vw
as
select name,test_func(date1) date1
from test_tbl
end
/

select * from test_vw --I get the desired result here. Date formatted as instructed
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
<<< Oracle has defined some rules/restrictions to use user-defined functions in select statement >>>
>>> You just wrapped oracle provided to_char() functions without violating rules/restrictions

Functions called from SQL have special restrictions

    * Stored in database
    * Accept only IN parameters
    * Accept and return only SQL datatypes (not PL/SQL specific datatypes)
    * Must use positional notation
    * Must own or have EXECUTE privilege
    * Cannot contain transactional or session control statements (eg. COMMIT, ROLLBACK, ALTER)
    * Cannot contain DDL - implies COMMIT
    * When used in SELECT statement - cannot contain DML
    * When used in UPDATE or DELETE - cannot SELECT or DML on same table

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.