473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

casting parameters to a function

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
2 1824

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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
5
by: el prinCipante | last post by:
I'm getting tired of the following error message. Compiler Error message : Error: Need explicit cast to convert from: float to: float * I am trying to use a routine from the Numerical Recipes...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
10
by: active | last post by:
Function CastIt (byval z as boolean) As ?? if z then CastIt = DirectCast(ActiveMdiChild, MyForm1) else CastIt = DirectCast(ActiveMdiChild, MyForm2) end if End Function Can I make a function...
5
by: Gene | last post by:
Hello all. Is the initializing assignment below ANSI standard- conforming? Is there a way to have the prefix parameters to ... type checked in such an assignment? E.g. in this case int x in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.