Re: cannot create function that uses variable table name

From: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
To: "Matthew Nuzum" <cobalt(at)bearfruit(dot)org>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: cannot create function that uses variable table name
Date: 2003-01-17 09:48:29
Message-ID: 200301170448.29838.darcy@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at){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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oliver Vecernik 2003-01-17 13:03:51 sum(time) problem
Previous Message Stephan Szabo 2003-01-17 04:43:49 Re: cannot create function that uses variable table name