From: | "Dean Gibson (DB Administrator)" <dba-sql(at)ultimeth(dot)net> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Including files |
Date: | 2003-05-08 19:32:51 |
Message-ID: | 5.1.0.14.2.20030508120123.00adeab8@dba-sql.ultimeth.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
I'm using PostgreSQL 7.3.2 (from RPM on this site) on RH Linux 7.
On several of the TABLEs I have, there are fields that contain codes. For example, one such field is a two-character state code (eg, 'CA' for 'California'). In order to convert these codes to their full names, I have a lookup TABLE:
CREATE TABLE states(
state_id CHAR( 2 ),
state_text CHAR(15)
);
In a query, I have the following SQL:
SELECT
...
address,
city,
(SELECT state_id || ' - ' || state_text::TEXT
FROM states
WHERE state_id = state
LIMIT 1)
AS state,
CASE WHEN substr( zip, 6 ) != ' ' THEN
substr( zip, 1, 5 ) || '-' || substr( zip, 6 )
ELSE
zip
END
AS zip,
...;
All this works fine! However, I would like to put the above (and many other code lookups) in VIEWs, and then JOIN the VIEWs. Unfortunately, the TABLE lookups are not deferred until after any JOINs (by contrast, the CASE statement above IS apparently deferred until after JOINs); this means that SELECTs based on such JOINed VIEWs can take a very long time.
So, this means that I need to put the above inner SELECT in a query or VIEW that does the JOIN, rather than in VIEWs that participate in the JOINs. This is turn means that I have proliferate the inner SELECT into quite a few VIEWs that do the various JOINS I do, rather than in just one VIEW.
OK, so I'd like to put the inner SELECT into another file, and simply include it as follows:
SELECT
...
address,
city,
/i stateSelect.sql
,
CASE WHEN substr( zip, 6 ) != ' ' THEN
substr( zip, 1, 5 ) || '-' || substr( zip, 6 )
ELSE
zip
END
AS zip,
...;
That means I have only one file to maintain for each such lookup table. Unfortunately, this does not work. While the documentation says, "Reads input from the file filename and executes it as though it had been typed on the keyboard.", this is not true. Apparently "\i" includes are not processed through the query buffer: in effect, the first part of the above select is stored in the query buffer, then the "\i stateSelect.sql" is read AND EXECUTED (giving an error), and then the remainder of the SELECT statement is read into the query buffer, and the SELECT statement is then executed without the included lines.
Which brings me to my question: is there any way to include files into the query buffer? Every other include facility that I've ever worked with, acts as though the included lines were typed AT THE POINT OF INCLUSION (like the docs infer).
My workaround is to manually copy the included portion into each required VIEW definition, but this creates quite a maintenance headache. Any suggestions (other than an awk script)?
Sincerely, Dean
From | Date | Subject | |
---|---|---|---|
Next Message | Alvaro Herrera | 2003-05-08 19:46:36 | Re: Disk usage |
Previous Message | Dennis Gearon | 2003-05-08 19:10:08 | Re: resequencing a user defined integer column |