pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedDROP IF ...
2+ messages / 2 participants
[nested] [flat]
* DROP IF ...
@ 2005-05-24 17:44 CG <cgg007@yahoo.com>
2005-05-24 22:07 ` Re: DROP IF ... Thomas F. O'Connell <tfo@sitening.com>
0 siblings, 1 reply; 2+ messages in thread
From: CG @ 2005-05-24 17:44 UTC (permalink / raw)
To: pgsql-sql
PostgreSQL 7.4 ...
I'm trying to find a way to drop a table via SQL if it contains 0 rows. Here
was my thought:
CREATE OR REPLACE FUNCTION dropif(text, bool)
RETURNS bool AS
'DECLARE
tblname ALIAS FOR $1;
condition ALIAS FOR $2;
BEGIN
IF (condition) THEN
EXECUTE(\'DROP TABLE "\' || tblname || \'";\');
END IF;
RETURN \'t\'::bool;
END;'
LANGUAGE 'plpgsql' VOLATILE;
... then ...
BEGIN;
CREATE TABLE testtbl (i int4);
SELECT dropif('testtbl',(SELECT count(*)::int4 FROM testtbl) = 0);
ERROR: relation 286000108 is still open
CONTEXT: PL/pgSQL function "dropif" line 6 at execute statement
... It makes sense. The select is still open when the table is going to be
dropped. I need a different strategy.
Please advise!
CG
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: DROP IF ...
2005-05-24 17:44 DROP IF ... CG <cgg007@yahoo.com>
@ 2005-05-24 22:07 ` Thomas F. O'Connell <tfo@sitening.com>
0 siblings, 0 replies; 2+ messages in thread
From: Thomas F. O'Connell @ 2005-05-24 22:07 UTC (permalink / raw)
To: cgg007@yahoo.com; +Cc: pgsql-sql
The following function takes a table name as a parameter and drops
the table and returns true if there are zero rows (otherwise, it
returns false):
CREATE OR REPLACE FUNCTION dropzero( varchar ) RETURNS bool AS '
DECLARE
zerotable ALIAS FOR $1;
zerocurs refcursor;
rowcount int;
BEGIN
OPEN zerocurs FOR EXECUTE ''SELECT COUNT( * ) FROM '' ||
zerotable;
FETCH zerocurs INTO rowcount;
CLOSE zerocurs;
IF rowcount = 0 THEN
EXECUTE ''DROP TABLE '' || zerotable;
RETURN true;
ELSE
RETURN false;
END IF;
END;
' LANGUAGE 'plpgsql';
-tfo
--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC
Strategic Open Source: Open Your i™
http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-260-0005
On May 24, 2005, at 12:44 PM, CG wrote:
> PostgreSQL 7.4 ...
>
> I'm trying to find a way to drop a table via SQL if it contains 0
> rows. Here
> was my thought:
>
> CREATE OR REPLACE FUNCTION dropif(text, bool)
> RETURNS bool AS
> 'DECLARE
> tblname ALIAS FOR $1;
> condition ALIAS FOR $2;
> BEGIN
> IF (condition) THEN
> EXECUTE(\'DROP TABLE "\' || tblname || \'";\');
> END IF;
> RETURN \'t\'::bool;
> END;'
> LANGUAGE 'plpgsql' VOLATILE;
>
> ... then ...
>
> BEGIN;
> CREATE TABLE testtbl (i int4);
> SELECT dropif('testtbl',(SELECT count(*)::int4 FROM testtbl) = 0);
>
> ERROR: relation 286000108 is still open
> CONTEXT: PL/pgSQL function "dropif" line 6 at execute statement
>
> ... It makes sense. The select is still open when the table is
> going to be
> dropped. I need a different strategy.
>
> Please advise!
>
> CG
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-05-24 22:07 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-05-24 17:44 DROP IF ... CG <cgg007@yahoo.com>
2005-05-24 22:07 ` Thomas F. O'Connell <tfo@sitening.com>
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