PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2002, 07:59 PM
dax dax is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: philippines
Posts: 4 dax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
array parameter in Function

Hi,
Please forgive me if would post this very novice question.... How to use an array parameter in function and how to call them? If you could give me an example I would appreciate it so much or any references....
Thanks a million in advance for your help and time...

Regards,
Dax

To rycamor:
First of all thank you for your advice in the array parameter
instead of increasing the number of parameter to be used..
I've created a table with this code :
-- Table: sample
CREATE TABLE sample (
code numeric(9, 0) NOT NULL,
name varchar(60),
age numeric(3, 0),
CONSTRAINT sample_pkey PRIMARY KEY (code)
) WITH OIDS;

...and created an insert stored function:
-- Function: ins_sample(_varchar)
CREATE FUNCTION ins_sample(_varchar) RETURNS int4 AS 'Declare
code ALIAS FOR $1[1];
name ALIAS FOR $1[2];
age ALIAS FOR $1[3];

BEGIN
insert into sample values
(to_number(code,"999999999"),name,to_number(age,"999"));
RETURN 0;
end;' LANGUAGE 'plpgsql';

But I cannot make this work.... Am I doing something wrong here?
How to execute the stored function? I've been trying to execute it by
using this statement but it gives me a parser error :
select ins_sample('{''1'', ''daxdtest'', ''12''}');

Thanks a lot in advance....

Last edited by dax : November 18th, 2002 at 01:07 AM.

Reply With Quote
  #2  
Old November 18th, 2002, 11:58 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Actually, this is not a 'novice' question here . I had to think about it a bit myself, and experiment with your code. It's going to take some time to explain it, though.

Before we get started, exactly what is the 'parse error' message you receive? What version of PostgreSQL are you running?

It is always best to be specific when asking these questions. Showing me the actual error message is much better than just telling me you have a parse error.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old November 19th, 2002, 01:17 AM
dax dax is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: philippines
Posts: 4 dax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
The error message occurs when I would try to run my insert function: select ins_sample('{''10'', ''daxdtest'', ''12''}') ... this would give me then: parse error at or near "[". Btw, my PostgreSQL version is 7.2.3.
Thanks for your help in advance....

Regards,
Dax

Reply With Quote
  #4  
Old November 19th, 2002, 10:41 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Here is the best I can deduce after testing (the documentation isn't too helpful here):

1. Apparently you cannot create aliases for array elements; only for the whole array:
Code:
CREATE FUNCTION ins_sample(_varchar) RETURNS int4 AS 'Declare
userinfo ALIAS FOR $1;
 BEGIN
insert into sample values (to_number(userinfo[1],"999999999"),userinfo[2],to_number(userinfo[3],"999"));
RETURN 0;
end;' LANGUAGE 'plpgsql';

Basically, there is little point in using aliases at all for array parameters, unless you have multiple arrays as incoming parameters. (Which you will probably have to do, after you read the next part)

2. PostgreSQL is not very happy with any attempts to cast array elements (to_number(), etc...). Basically, whatever was your array type on creation, you are stuck with that. So, if you want parameters of different types, you will have to use different arrays:
Code:
CREATE FUNCTION ins_sample(_varchar, _int4) RETURNS int4 AS 'Declare
textinput ALIAS FOR $1;
numberinput ALIAS FOR $2;
 BEGIN
insert into sample values (numberinput[1],textinput[1],numberinput[2]);
RETURN 0;
end;' LANGUAGE 'plpgsql';


3. This all seems like too much work, to me. If you really need a stored procedure just to insert data into a table, why not just create a function inside your programming environment to do that?

4. Or, for another work-around, try installing the Perl procedural language handler. Then you can just send one large string as a parameter, with delimiters (commas, tabs, whatever), and use Perl's string-handling functions to break it up into different elements to insert.

5. Finally, I don't understand what you are trying to accomplish anyway. Why is "SELECT ins_table(value1,value2,value3)" better than just doing "INSERT INTO table VALUES(value1,value2,value3)"? If you are trying to get one function to insert into multiple tables, you should just create a view which joins those tables, and create a query rewrite rule that allows for inserts into that view (the rule actually 'takes apart' your query and puts each value in its appropriate table). See the following for more info on query rewrite rules:

http://www.postgresql.org/idocs/ind...createrule.html
http://www.ca.postgresql.org/docs/a...ok/node124.html

Reply With Quote
  #5  
Old November 19th, 2002, 11:29 PM
dax dax is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: philippines
Posts: 4 dax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
Thanks for your advice and thanks a million to you. Just for a background of what I'm try to achieve: I have an application in which the user's can choose what back-end they want either SQLServer 2000, Oracle 9i or PostgreSQL (the application by the way is in VB.Net). So I created a DAL (Data Access Layer) to which would interact to the database (in which the user choose) so that my BL (Business Layer) would not mind which database the application is using. But the probelm is these databases has a specific SQL stataments or process in doing things so I opted that all of my insert and other database interaction would be done in the Databases, so that I would have a uniform of calling things and would not mind what my back-end is....
Anyway, THANKS A MILLION FOR THE ADVICE AND TIPS. And MORE POWER TO YOU!!!!!

Regards,
Dax

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > array parameter in Function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap