By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

casting parameters to a function

P: n/a
I am trying to move all of the sql statements from my code into a pl/pgsql
function. I have one function for a transaction block that contains 10
inserts and I need to pass 50 parameters to it.

Well I can't do that, so I was playing around with passing the parameters in
as a varchar[] array, but then I have the issue of the variables being the
wrong type when I insert them.

I was playing around with trying to convert types, but it isn't working so
far. The function takes a single varchar[] as an argument..

This doesn't work:
s ALIAS FOR $1;
in_trans_date timestamp := s[3];

and this doesn't work either:
in_trans_date timestamp := $1[3];

Can someone help me in how to assign an array element to a variable and cast
it?
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

I finally figured out that you cannot assign parameters to a variable in the
DECLARE section, it has to be done inside the BEGIN/END block. except for
the ALIAS command it seems that parameters do not exist in the DECLARE
block.

I am trying to move all of the sql statements from my code into a pl/pgsql
function. I have one function for a transaction block that contains 10
inserts and I need to pass 50 parameters to it.

Well I can't do that, so I was playing around with passing the parameters in as a varchar[] array, but then I have the issue of the variables being the
wrong type when I insert them.

I was playing around with trying to convert types, but it isn't working so
far. The function takes a single varchar[] as an argument..

This doesn't work:
s ALIAS FOR $1;
in_trans_date timestamp := s[3];

and this doesn't work either:
in_trans_date timestamp := $1[3];

Can someone help me in how to assign an array element to a variable and cast it?
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #2

P: n/a
"Chris Ochs" <ch***@paymentonline.com> writes:
I was playing around with trying to convert types, but it isn't working so
far. The function takes a single varchar[] as an argument.. This doesn't work:
s ALIAS FOR $1;
in_trans_date timestamp := s[3]; and this doesn't work either:
in_trans_date timestamp := $1[3];


These should work, and do work in CVS tip, but in 7.4 and before you'll
need to do the assignment separately from the variable declaration, eg

DECLARE
s ALIAS FOR $1;
in_trans_date timestamp;
BEGIN
in_trans_date := s[3];
...

PITA, I know, but the previous coding didn't allow any variable
references in initialization expressions. If it really bugs you
you could drop the CVS-tip version of src/pl/plpgsql/src/gram.y
into 7.4 to get the fix:

2003-12-22 19:01 tgl

* src/pl/plpgsql/src/gram.y: Allow plpgsql variables' default value
expressions to reference existing variables (such as function
parameters). Per gripe from David Fetter.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.