Re: sub query reference error or user error

From: David Johnston <polobo(at)yahoo(dot)com>
To: sivakumar krishnamurthy <sivakumar(dot)mailinglist(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: sub query reference error or user error
Date: 2012-11-06 18:22:29
Message-ID: AD488835-DD56-4B7D-9137-033137F8015D@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Nov 6, 2012, at 12:36, sivakumar krishnamurthy <sivakumar(dot)mailinglist(at)gmail(dot)com> wrote:

> Hi All,
> For the below test case shouldn't the update statement throw error because the sub query projects a column which is not existing in the table. For ex in inner select nos1 column exists in t1 and not t2.
> Is this a bug or an OP error?
>
> template1=# SELECT version();
> version
> ----------------------------------------------------------------------------------------------------------------
> PostgreSQL 9.1.6 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3, 64-bit
> (1 row)
> template1=# \d t1
> Table "public.t1"
> Column | Type | Modifiers
> --------+---------+-----------
> nos1 | integer |
>
> template1=# \d t2
> Table "public.t2"
> Column | Type | Modifiers
> --------+---------+-----------
> nos2 | integer |
>
> template1=# UPDATE t1 set nos1=2 where nos1 in (select nos1 from t2);
> UPDATE 0
> template1=#
>
> Thanks,
> Sivakumar.K
>

In a correlated sub-query any column from the parent or sub-query can appear. In this case anything in t1or t2. In this case the effect is a constant true evaluation since for each row in t1 the value of nos1 is constant. The use of t2 simply causes the constant to be repeated once for each row in t2. As a safety feature you should prefix your columns in sub-queries with the proper table name. t2.nos1 would then throw the desired error.

Dave J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lists 2012-11-06 18:48:31 Re: Unexpectedly high disk space usage
Previous Message Achilleas Mantzios 2012-11-06 17:49:41 SPI function varchar difference between 9.0 and 9.2