Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:18:32+00:00 2026-05-27T21:18:32+00:00

I have the following UPDATE statement: update mytable set a = first_part(genid()), b =

  • 0

I have the following UPDATE statement:

update mytable
   set a = first_part(genid()),
       b = second_part(genid()),
       c = third_path(genid())
 where package_id = 10;

In this example the function genid() is called three times for each row, which is wrong – I want it to be called only once for each row of mytable.

I’m using PostgreSQL 8.4 database. How to write the correct update?

I’ve tried something like this:

update mytable
   set a = first_part(g),
       b = second_part(g),
       c = third_path(g)
 where package_id = 10
  from genid() as g;

But it didn’t work, because genid() has been called only once for the whole update statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:18:33+00:00Added an answer on May 27, 2026 at 9:18 pm

    Have you tried Postgres’ non-standard UPDATE .. FROM clause? I imagine, this would work

    update mytable
       set a = first_part(gen.id),
           b = second_part(gen.id),
           c = third_path(gen.id)
      from (
              select genid() as genid, id
              from mytable 
              where package_id = 10
           ) gen
     where mytable.id = gen.id;
     --and package_id = 10 -- This predicate is no longer necessary as the subquery
                           -- already filters on package_id, as Erwin mentioned
    

    Note that I’m forcing genid() to be called exactly once per record in mytable within the subselect. Then I’m self-joining mytable and gen using a hypothetical id column.
    See the documentation here:

    http://www.postgresql.org/docs/current/interactive/sql-update.html

    This seems to have been introduced with Postgres 9.0 only, though. If that seems too complicated (i.e. not very readable), you can still resort to pgplsql as user Florin suggested here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have the following update statement: update db1.dbo.table1 set db1.dbo.table1.col = db1_bk.dbo.table1.col where db1.dbo.table1.id
I have the following update statement : UPDATE D SET D.Xtra1 = CASE when
I have the following SQL statement where i'm trying to update multiple rows matching
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have the following javascript code, which loads without error, however the update function
Update: This does work, I was being stupid :( i have the following extension
I have the following select statement that finishes almost instantly. declare @weekending varchar(6) set
I have a question about TSQL function Update. For example, I have a table
I am updating my table with the following statement: UPDATE table SET percentage =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.