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 9021397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:14:47+00:00 2026-06-16T05:14:47+00:00

I have a problem with SQL statement which is a part of long transaction.

  • 0

I have a problem with SQL statement which is a part of long transaction.

UPDATE tab_1
LEFT JOIN tab_2
ON tab_1.id = tab_2.tab_1_id
SET tab_1.something = 42
WHERE tab1.id = tab_2.tab_1_id;

Everything is simple and works fine as long as tab_1 and tab_2 exist in database, which is obvious. 😉

The problem is that the transaction have to be commited on 4 different servers, and tab_2 is “dynamic” table, which may exist in specific db / db schema, or not…

If tab_2 don’t exists, database threw exception and the whole transaction is not commited. What I want is to continue anyway (just update 0 rows)!

I have tried something like this:

UPDATE [all the same as above] WHERE tab1.id = tab_2.tab_1_id AND EXISTS (select 1 from pg_class where relname='tab_2');

…but it’s still wrong, since “exception-check” is made before “where” condition (it’s the same table we want to use in join..).

Is there any way to do this with “pure” SQL? 🙂

Something like: LEFT JOIN tab_2 IF tab_2 EXISTS (and if not – do nothing, return null, etc.?)

I know there is a way to do this in pl/pgsql procedure.
The second possibility is to create table if not exists, before the statement..

But maybe there is some kind of simple and elegant way to do this in one statement? 🙂

DBMS: PostgreSQL 9.2

  • 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-06-16T05:14:48+00:00Added an answer on June 16, 2026 at 5:14 am

    I don’t think an UPDATE statement that succeeds even if a table doesn’t exist is simple and elegant. I think it is strange and confusing.

    Why not just include a conditional that checks whether that table exists, and only perform the update if it exists? It would be a lot clearer.

    Another option would be to create a view that points to tab_2 if it exists, or points to an empty table otherwise. This could be helpful if you have a lot of queries like this, and you don’t want to change them all.

    Update: Here is what a condition would look like (has to be within a function or a BEGIN...END block):

    IF EXISTS (select 1 from pg_class where relname='tab_2') THEN 
       UPDATE...
    END IF;
    

    Depending on the details of Postgresql, this still might fail compilation if it sees a table that doesn’t exist in the UPDATE statement (I’m not a Postgresql user). In that case, you would need to create a view that points to tab_2 if it exists, and an empty table if it doesn’t exist..

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

Sidebar

Related Questions

So basically my problem is this. I have an sql statement which is obtained
I have a problem which i have simplified below. I have a sql statement
I have a problem with the SQL statement detailed below. The query returns the
I have a problem with a sql-statement. My DB is MySQL 5.1. I have
I have an big problem with an SQL Statement in Oracle. I want to
I have a problem creating the following SQL Statement using LINQ & C# select
I have a complex sorting problem with my SQL statement. I have a table
I have a simple problem with SQL SERVER charindex function. DECLARE @VAR1 varchar SET
Using a stored procedure i have a fairly complex SQL statement which returns a
Possible Duplicate: using dynamic IN clause in MSSQL I have one SQL statement which

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.