Re: Is there a way to elegantly do a : CREATE TEMP TABLE X AS SELECT .... ON COMMIT DROP

From: Colin Wetherbee <cww(at)denterprises(dot)org>
To: Adrian Klaver <aklaver(at)comcast(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to elegantly do a : CREATE TEMP TABLE X AS SELECT .... ON COMMIT DROP
Date: 2008-03-13 21:51:53
Message-ID: 47D9A1F9.5090301@denterprises.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adrian Klaver wrote:
> On Thursday 13 March 2008 2:46 pm, Colin Wetherbee wrote:
>> Adrian Klaver wrote:
>>> On Thursday 13 March 2008 4:49 am, David Gagnon wrote:
>>>> Hi all,
>>>>
>>>> I think the title says everything:-)
>>>>
>>>> I just what a way to create a TEMP for the current transaction only.
>>>> If possible I don't want to create the TEMP table first, specify all
>>>> column types, etc.
>>>>
>>>> CREATE TEMP TABLE _T_CR1 AS
>>>> SELECT CRNUM, CRYPNUM, CRMONT, CRDATE, GLNUM, CRRRNUM, CRACNUM, GLDESC
>>>> FROM CR INNER JOIN CS ON CR.CRNUM = CS.CSCRNUM AND CR.CRYPNUM =
>>>> CS.CSYPNUM INNER JOIN GL ON CS.CSGLNUM = GL.GLNUM AND 1 = GL.GLSOCTRL
>>>> WHERE CRYPNUM = companyId
>>>> AND DATEDIFF(DY, CRDATE, GETDATE()) >= _AGELIMITE
>>>> ON COMMIT DROP;
>>> I am missing something here. What is wrong with the above statement?
>> You're missing:
>>
>> cww=# BEGIN;
>> BEGIN
>> cww=# CREATE TEMP TABLE foo AS (SELECT 1 AS a, 2 AS b) ON COMMIT DROP;
>> ERROR: syntax error at or near "ON" at character 50
>> LINE 1: CREATE TEMP TABLE foo AS (SELECT 1 AS a, 2 AS b) ON COMMIT D...
>> ^
>
> Try CREATE TEMP TABLE foo ON COMMIT DROP AS (SELECT 1 AS a, 2 AS b) ;

I don't know what version the OP is using, but that doesn't work on 8.1:

cww=# CREATE TEMP TABLE foo ON COMMIT DROP AS (SELECT 1 AS a, 2 AS b);
ERROR: syntax error at or near "ON" at character 23
LINE 1: CREATE TEMP TABLE foo ON COMMIT DROP AS (SELECT 1 AS a, 2 AS...
^

I'm jumping in here because temporary ON COMMIT DROP tables created with
AS (SELECT ...) would be handy for me, too. :)

Colin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-03-13 22:19:50 Re: Is there a way to elegantly do a : CREATE TEMP TABLE X AS SELECT .... ON COMMIT DROP
Previous Message Adrian Klaver 2008-03-13 21:49:06 Re: Is there a way to elegantly do a : CREATE TEMP TABLE X AS SELECT .... ON COMMIT DROP