Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id C3AF2F6332A for ; Thu, 19 Jul 2012 07:28:43 -0300 (ADT) Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SrnyM-0000qN-IU for pgsql-admin@postgresql.org; Thu, 19 Jul 2012 10:28:43 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Srny6-0008C9-JL for pgsql-admin@postgresql.org; Thu, 19 Jul 2012 12:28:26 +0200 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 19 Jul 2012 12:28:26 +0200 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 19 Jul 2012 12:28:26 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Thomas Kellerer Subject: Re: need help to write a function in postgresql Date: Thu, 19 Jul 2012 12:28:03 +0200 Lines: 63 Message-ID: References: <000b01cd658e$e66ae920$b340bb60$@avineonindia.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <000b01cd658e$e66ae920$b340bb60$@avineonindia.com> X-Pg-Spam-Score: -0.7 (/) X-Archive-Number: 201207/41 X-Sequence-Number: 39758 Madhu.Lanka, 19.07.2012 11:14: > Hi Friends > > Can u please help to write a function for the following scenario? > > I have 3 table’s user_roles, principals and roles. > > I have to write a function in postgresql which should excepts 2 parameters (name, password) > > With those 2 parameters the query should be executed and return the result.(Result of the query will be 1 or more rows). > > The query is working fine if executed it manually by hardcoding the values for name and password, but is as to be written in function so that I can send different values. > > “select u.role_id,u.user_id,p.name,p.creation_date,p.telephone_number,p.email,p.remarks,p.city,p.state,p.country,p.postal_address,p.company_name,p.department_name,p.contact_person, > > p.role_group,p.role_description,p.first_name,p.last_name,p.pincode,p.status,p.department_id,p.designation_id,r.role_name,r.right_edit,r.right_filter,r.right_search,r.right_browse, > > r.right_print, r.right_download,r.right_admin_user,r.right_image_creator,r.right_vector_download,r.right_wps,r.right_queries_filter,r.right_google_map,r.right_user_management, > > r.right_route_analysis, r.right_legend,r.right_treeview,r.insert,r.update,r.delete,r.query,r.lock from user_roles u,.principals p,roles r where p.PRINCIPAL_ID = u.USER_ID and > > r.role_id = u.role_id and p.name=? and p.password=?” > Something like this: create or replace function retrieve_user(uname text, pwd text) returns table (role_id integer, user_id integer, name text, .... more columns ...) as $$ SELECT u.role_id, u.user_id, p.name, p.creation_date, p.telephone_number, .... more columns ... FROM user_roles u, principals p, roles r WHERE p.principal_id = u.user_id AND r.role_id = u.role_id AND p.name = $1 AND p.password = $2 $$ language SQL; Then you can do: select * from retrieve_user('foo', 'bar'); Personally I'd prefer to create view that wraps that select statement and then simply do a select * from user_view where name = 'foo' and password = 'bar' Regards Thomas