Re: cannot create function that uses variable table name - Mailing list pgsql-sql

From D'Arcy J.M. Cain
Subject Re: cannot create function that uses variable table name
Date
Msg-id 200301170448.29838.darcy@druid.net
Whole thread Raw
In response to cannot create function that uses variable table name  ("Matthew Nuzum" <cobalt@bearfruit.org>)
List pgsql-sql
On Thursday 16 January 2003 22:32, Matthew Nuzum wrote:
> I have a number of tables in my database that use the concept of
> “display order”, which is a field that can be used in an order by clause
> to dictate what order the results should come out in.
>  
> I thought I would be crafty and devise a function that would always
> return the highest numbered item in the table.  But it doesn’t work.  It
> always gives me a parse error at $1.  Here’s the function:

I may be wrong but aren't you trying to do something like this?

INSERT INTO files (accountid, filename, dsply_order) VALUES ('account2', 'Testing',   (SELECT
COALESCE(MAX(dsply_order),0) + 1 FROM files      WHERE accountid = 'account2'));
 

Alternatively, assuming that fileid is a serial number, why not just use that 
in your order by clause.  I assume that you want something like this.

SELECT * FROM files WHERE accountid = 'account2' ORDER BY dsply_order;

This should give you exactly the same result:

SELECT * FROM files WHERE accountid = 'account2' ORDER BY fileid.

It all depends on what problem exactly you are trying to solve of course.

-- 
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: cannot create function that uses variable table name
Next
From: Oliver Vecernik
Date:
Subject: sum(time) problem