pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Markus Wagner <wagner@imsd.uni-mainz.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: using SQL to evaluate arbitrary expressions?
Date: Tue, 02 Oct 2001 01:07:41 -0400
Message-ID: <6997.1001999261@sss.pgh.pa.us> (raw)
In-Reply-To: <01100206321504.01063@magnus>
References: <01100206321504.01063@magnus>

Markus Wagner <wagner@imsd.uni-mainz.de> writes:
> We are glad to have found out that we can evaluate such expressions 
> simply by calling them with SELECT, e. g. "SELECT 29 - 2 > 18" returns true. 
> We think that it would be very nice to benefit from the pg database system 
> parser. But can we rely on this functionality in the future? Is this
> standard SQL?

Well, it's not standard SQL: SQL92 requires a FROM clause in SELECT.
But I can assure you it won't disappear from Postgres; it's too darn
useful.

The customary workaround, as seen in systems like Oracle, is to have
a dummy table containing exactly one row; Oracle calls the dummy table
DUAL.  (Anyone know why?  Seems like SINGLE would be more sensical...)
Then you can do

	SELECT 29 - 2 > 18 FROM dual;

The fact that your expression doesn't actually *use* any values from
the one table row doesn't bother the system; you get one evaluation
of the expression, you're happy, and so is the SQL standard.

My advice is to just write "SELECT 29 - 2 > 18", because it's clearer
in intent, simpler, and some tiny fraction faster than specifying
a useless table reference.  But you can take comfort in the fact that
there's a simple workaround if you're ever forced to use a DBMS that
won't accept this syntax.

			regards, tom lane



view thread (2+ messages)

Message-ID: <6997.1001999261@sss.pgh.pa.us>
Permalink:  ../6997.1001999261@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/6997.1001999261@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, wagner@imsd.uni-mainz.de
  Subject: Re: using SQL to evaluate arbitrary expressions?
  In-Reply-To: <6997.1001999261@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox