Thread: passing strings to functions

passing strings to functions

From
Ivan Sergio Borgonovo
Date:
I can't understand how to pass strings to functions.

create type tSession
  as
(
    SessionCode char(32),
    SessionID char(32),
    UserIDI integer,
    SessionN integer
);

create or replace function GetSessionCode( char(32), smallint,
boolean, varchar(128) )    returns tSession as
'
declare
  newSession tSession;
...
begin
...
  return newSession;
end;
' language plpgsql;

test1=# select * from
GetSessionCode('12345678901234567890123456789012',1,TRUE,'sadas');

ERROR: function getsessioncode("unknown", integer, boolean,"unknown")
does not exist HINT:  No function matches the given name and argument
types. You may need to add explicit type casts.

Is there a simpler way than casting everytime?

Curiously this work:

create or replace function testa( char(10) )
    returns char(32) as
'
begin
    return md5(''aaaa'');
end;
' language plpgsql;

test1=# select * from testa('dsd');
              testa
----------------------------------
 74b87337454200d4d33f80c4663dc5e5
(1 row)


thx

Re: passing strings to functions

From
Tom Lane
Date:
Ivan Sergio Borgonovo <mail@webthatworks.it> writes:
> I can't understand how to pass strings to functions.

Your problem isn't with the strings, it's with the smallint parameter.
The undecorated "1" is an integer not a smallint, and it won't be
silently cast in this particular scenario.

> Is there a simpler way than casting everytime?

I'd suggest declaring the function to take integer.  If you really want
the rowtype to use smallint then you can cast internally.

            regards, tom lane