1. Matt Balent
  2. PowerBuilder
  3. Tuesday, 11 June 2024 14:07 PM UTC

Anyone using Postgresql - how are you obtaining the @@identity value of the last inserted record? (similar to SCOPE_IDENTITY in SQL Server).  There is an insert statement with the 'RETURNING id' clause but inline SQL in PB doesn't like this syntax.




Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 11 June 2024 17:39 PM UTC
  2. PowerBuilder
  3. # 1

Hi Matt;

  If Mike's suggestion does not work .. try a ReSelectRow() command after the insert.  HTH

Regards .. Chris

  1. mike S
  2. Tuesday, 11 June 2024 18:13 PM UTC
reselectrow only works if the id is not the primary key....

  1. Helpful
  1. Chris Pollach @Appeon
  2. Tuesday, 11 June 2024 18:36 PM UTC
Hi Mike .. FWIW: This has always worked for me before. But, I have never tried it with the PG DBMS.
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Tuesday, 11 June 2024 14:34 PM UTC
  2. PowerBuilder
  3. # 2

this is an old stack overflow, so maybe things have changed:  insert - PostgreSQL function for last inserted ID - Stack Overflow

this is basically saying to use sequences directly since that is what postgresql is really doing anyway. 

Using sequences directly is some extra code, but it will allow you to get the sequence prior to saving the rest of it.  If not, you would get it after using one of the following:

currval() (needs the sequence name) only works after an INSERT (which has  executed nextval() ), in the same session.


This is similar to the previous, only that you don't need to specify the sequence name: it looks for the most recent modified sequence (always inside your session, same caveat as above).


There are no comments made yet.
  • Page :
  • 1

There are no replies made for this question yet.
However, you are not allowed to reply to this question.