pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with curval
3+ messages / 3 participants
[nested] [flat]

* Problem with curval
@ 2004-11-11 15:35  Mike Fahey <mfahey@enter.net>
  0 siblings, 2 replies; 3+ messages in thread

From: Mike Fahey @ 2004-11-11 15:35 UTC (permalink / raw)
  To: pgsql-odbc

This wont work because myvalue must be a record or row variable.

I'm guessing its nearly impossible to get the last inserted id from 
access 2003 using
postgres odbc. 

select @@IDENTITY always returns "2".

Thoughts?




create function last_insert_id(varchar,varchar ) returns integer
 as '
declare
intable alias for $1;
incolumn alias for $2;
myvalue integer;

BEGIN

for myvalue in execute  '' select  currval('''' ''
 || intable || ''_'' || incolumn
 || ''_seq'''')
'' Loop
    return myvalue;


END loop;
END;

'
 language plpgsql;



-- 
With best regards,

Mike Fahey - Systems Administration
********************************************************************
      ENTER.NET - "The Road to the Internet Starts Here!" (tm)
  (610) 437-2221 * http://www.enter.net/ * email:support@enter.net
********************************************************************




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with curval
@ 2004-11-11 21:19  Jeff Eckermann <jeff_eckermann@yahoo.com>
  parent: Mike Fahey <mfahey@enter.net>
  1 sibling, 0 replies; 3+ messages in thread

From: Jeff Eckermann @ 2004-11-11 21:19 UTC (permalink / raw)
  To: Mike Fahey <mfahey@enter.net>; pgsql-odbc


--- Mike Fahey <mfahey@enter.net> wrote:

> This wont work because myvalue must be a record or
> row variable.
> 
> I'm guessing its nearly impossible to get the last
> inserted id from 
> access 2003 using
> postgres odbc. 
> 
> select @@IDENTITY always returns "2".
> 
> Thoughts?
> 
> 
> 
> 
> create function last_insert_id(varchar,varchar )
> returns integer
>  as '
> declare
> intable alias for $1;
> incolumn alias for $2;
> myvalue integer;

myvalue record;

> 
> BEGIN
> 
> for myvalue in execute  '' select  currval('''' ''
>  || intable || ''_'' || incolumn
>  || ''_seq'''')
> '' Loop

as myalias'' Loop

>     return myvalue;

return myvalue.myalias;

(you may need to do some casting, but I suspect that
plpgsql will take care of that for you)

> 
> 
> END loop;
> END;
> 
> '
>  language plpgsql;
> 
> 
> 
> -- 
> With best regards,
> 
> Mike Fahey - Systems Administration
>
********************************************************************
>       ENTER.NET - "The Road to the Internet Starts
> Here!" (tm)
>   (610) 437-2221 * http://www.enter.net/ *
> email:support@enter.net
>
********************************************************************
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> majordomo@postgresql.org
> 



		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Problem with curval
@ 2004-11-14 14:20  Sim Zacks <sim@nospam.com>
  parent: Mike Fahey <mfahey@enter.net>
  1 sibling, 0 replies; 3+ messages in thread

From: Sim Zacks @ 2004-11-14 14:20 UTC (permalink / raw)
  To: pgsql-odbc

I am using something similar to the following code in Access to get the
currval:
        conn.Execute ("Insert into Shipments(...) values(...)")
        rs.Open "select currval('shipments_shipmentid_seq') as shipmentid;",
conn, adOpenForwardOnly, adLockReadOnly
where rs is my ADODB.RecordSet and conn is an ADODB.Connection

In your case it looks like you're trying to build a function to mimic the
MySQL last_insert_id function.
You should not be using a loop. You only want one value.
your code should look like:

create or replace function last_insert_id(varchar,varchar ) returns integer
  as
'
 declare
 intable alias for $1;
 incolumn alias for $2;
 myvalue integer;
begin
    select  currval('''' ''  || intable || ''_'' || incolumn || ''_seq'''')
into myvalue;
    return myvalue;
end
'
 language plpgsql;

Your SQL code to retrieve it wsould be simply "select
last_insert_id(var1,var2)"


"Mike Fahey" <mfahey@enter.net> wrote in message
news:419386D5.9060502@enter.net...
> This wont work because myvalue must be a record or row variable.
>
> I'm guessing its nearly impossible to get the last inserted id from
> access 2003 using
> postgres odbc.
>
> select @@IDENTITY always returns "2".
>
> Thoughts?
>
>
>
>
> create function last_insert_id(varchar,varchar ) returns integer
>  as '
> declare
> intable alias for $1;
> incolumn alias for $2;
> myvalue integer;
>
> BEGIN
>
> for myvalue in execute  '' select  currval('''' ''
>  || intable || ''_'' || incolumn
>  || ''_seq'''')
> '' Loop
>     return myvalue;
>
>
> END loop;
> END;
>
> '
>  language plpgsql;
>
>
>
> -- 
> With best regards,
>
> Mike Fahey - Systems Administration
> ********************************************************************
>       ENTER.NET - "The Road to the Internet Starts Here!" (tm)
>   (610) 437-2221 * http://www.enter.net/ * email:support@enter.net
> ********************************************************************
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-11-14 14:20 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-11-11 15:35 Problem with curval Mike Fahey <mfahey@enter.net>
2004-11-11 21:19 ` Jeff Eckermann <jeff_eckermann@yahoo.com>
2004-11-14 14:20 ` Sim Zacks <sim@nospam.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox