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

  • SEARCH
  • Home
  • 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 6550347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:12:18+00:00 2026-05-25T12:12:18+00:00

Possible Duplicate: Insert, on duplicate update (postgresql) Cannot SELECT from UPDATE RETURNING clause in

  • 0

Possible Duplicate:
Insert, on duplicate update (postgresql)
Cannot SELECT from UPDATE RETURNING clause in postgres

Help to understand me syntax error. I try to make such implementation of upsert query in
PosgreSql:

create table tbl( key  int, val int);

insert into tbl(key,val)
   select distinct(key), 0 from unnest('{0,1,2,3,4,5}'::int[]) as key
       where key not in (
         update tbl set val = 1
             where key = any('{0,1,2,3,4,5}'::int[])
         returning key
);

error is :

ERROR:  syntax error at or near "tbl"
ROWS 6:  update tbl set val = 1
                  ^

********** Error **********

ERROR: syntax error at or near "tbl"
SQL state: 42601
Character: 167

But update subquery without insert part work well.

Is any easiest way to make upsert query?

  • 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-25T12:12:18+00:00Added an answer on May 25, 2026 at 12:12 pm

    Your question was suggested as a duplicate because a solution to PostgreSQL not having an UPSERT command was already posted, which does answer your question of how to implement UPSERT.

    In answer to your syntax error question you are trying to do is not currently possible. You will find that a variation of it is possible in the upcoming version 9.1 of PostgreSQL, which will support data modifying statements in a WITH clause:
    http://www.postgresql.org/docs/9.1/static/queries-with.html#QUERIES-WITH-MODIFYING

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

Sidebar

Related Questions

Is it possible to INSERT...ON DUPLICATE KEY UPDATE into a table on the current
Possible Duplicate: How do I update if exists, insert if not (aka upsert or
Possible Duplicate: Correct INSERT .. ON DUPLICATE KEY syntax? Which is the correct syntax
Possible Duplicate: How do I update if exists, insert if not (aka upsert or
I wonder if it is possible to restrain users to insert duplicate registration records.
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: insert multiple rows via a php array into mysql I know about
Possible Duplicate: How do I insert a linebreak where the cursor is without entering
Possible Duplicate: Integer out of range on Postgres DB When my code tries to
Possible Duplicate: How to get the insert ID in JDBC? Hi, I'm using JDBC

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.