Unix Technical Forum

User Define Functions

This is a discussion on User Define Functions within the pgsql Admins forums, part of the PostgreSQL category; --> Hi, I have created the database with 3 schemas. Example : Schema names : Newyorlk, Atlanta,California Each schema has ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 06:02 AM
Lathika Wijerathne
 
Posts: n/a
Default User Define Functions

Hi,



I have created the database with 3 schemas.

Example : Schema names : Newyorlk, Atlanta,California



Each schema has a similar tables. But there can be schema specific tables
too.



I want to create a user define functions to retrieve information.



----------------------------------------------------------------------

CREATE OR REPLACE FUNCTION Newyorlk.get_customer_name (custid integer,
invoice integer)

RETURNS varchar AS

$$



DECLARE

m_display varchar (100);

m_status varchar(10);

BEGIN



SELECT cname INTO m_display from Newyorlk.CUSTOMER

WHERE id = custid;



SELECT status INTO m_status from Newyorlk.CUSTOMER_INVOICE

WHERE id = invoice;



m_display := m_display||'-'|| m_status ;



RETURN m_display;

END;

$$

LANGUAGE 'plpgsql' VOLATILE;

--------------------------------------------------------------------



In the above function if I change Newyorlk.CUSTOMER to CUSTOMER . It gives
a runtime error saying table not available.

I think when I remove the schema name, posgresSql checks the table in the
public schema.



But in Oracle, when we give the schema name with the function name, it
assumes all the objects within the functions are referring to that schema.

Do you know any other way to do a user define function, without hard cording
schema name each time when referring to a table.



Thanks in Advance

Lathika






Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 06:02 AM
Gnanavel S
 
Posts: n/a
Default Re: User Define Functions

use
set search_path = yourschemaname
to set the schema before calling the function

On 7/27/05, Lathika Wijerathne <lathika@rezgateway.com> wrote:
>
> Hi,
>
> I have created the database with 3 schemas.
>
> Example : Schema names : Newyorlk, Atlanta,California
>
> Each schema has a similar tables. But there can be schema specific tables
> too.
>
> I want to create a user define functions to retrieve information.
>
> ----------------------------------------------------------------------
>
> CREATE OR REPLACE FUNCTION Newyorlk.get_customer_name (custid integer,
> invoice integer)
>
> RETURNS varchar AS
>
> $$
>
> DECLARE
>
> m_display varchar (100);
>
> m_status varchar(10);
>
> BEGIN
>
> SELECT cname INTO m_display from Newyorlk.*CUSTOMER*
>
> WHERE id = custid;
>
> SELECT status INTO m_status from Newyorlk.*CUSTOMER_INVOICE*
>
> WHERE id = invoice;
>
> m_display := m_display||'-'|| m_status ;
>
> RETURN m_display;
>
> END;
>
> $$
>
> LANGUAGE 'plpgsql' VOLATILE;
>
> --------------------------------------------------------------------
>
> In the above function if I change Newyorlk.*CUSTOMER to CUSTOMER . *It
> gives a runtime error saying table not available.
>
> I think when I remove the schema name, posgresSql checks the table in the
> public schema.
>
> But in Oracle, when we give the schema name with the function name, it
> assumes all the objects within the functions are referring to that schema..
>
> Do you know any other way to do a user define function, without hard
> cording schema name each time when referring to a table.
>
> Thanks in Advance
>
> Lathika
>
>



--
with regards,
S.Gnanavel
Satyam Computer Services Ltd.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:28 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com