473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

question about temp table in function

I'm working on a plpgsql function that creates and populates a
temporary table. I would like the function to first drop the temp
table, if it already exists. I'm not sure how to accomplish this,
though. My first inclination was to simply wrap the 'drop table'
command in an exception handling block and ignore the exception if the
table does not exist. I'm not sure what error condition to catch,
though, so rather than specifying a "WHEN" condition, I just had
something like the following:

begin
execute 'drop table my_temp';
exception
-- do nothing
end;

That didn't work; apparently the "WHEN" condition is necessary. What
condition should I be trapping for?

Is there a better way to accomplish this? I thought about querying
pg_tables, and seeing if a record exists. Would that be a better
approach?

Thanks,
Tim
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 4606
On Tue, Nov 16, 2004 at 01:39:45PM -0600, Timothy Perrigo wrote:
begin
execute 'drop table my_temp';
exception
-- do nothing
end;

That didn't work; apparently the "WHEN" condition is necessary. What
condition should I be trapping for?


I assume you're using one of the 8.0 betas; earlier versions of
PostgreSQL didn't have exception handling.

Appendix A of the documentation shows the conditions you can trap.
Here's a way to find out what exception you need without having to
search the entire list:

CREATE OR REPLACE FUNCTION foo() RETURNS BOOLEAN AS $$
BEGIN
DROP TABLE my_temp;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql;

\set VERBOSITY verbose
SELECT foo();
ERROR: 42P01: table "my_temp" does not exist
CONTEXT: SQL statement "DROP TABLE my_temp"
PL/pgSQL function "foo" line 2 at SQL statement
LOCATION: DropErrorMsgNonExistent, utility.c:144

The error code is 42P01, which Appendix A shows as UNDEFINED TABLE.
The exception-handling block would therefore be:

BEGIN
DROP TABLE my_temp;
EXCEPTION
WHEN undefined_table THEN
NULL;
END;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2

On Nov 16, 2004, at 2:11 PM, Michael Fuhr wrote:
\set VERBOSITY verbose
SELECT foo();
ERROR: 42P01: table "my_temp" does not exist
CONTEXT: SQL statement "DROP TABLE my_temp"
PL/pgSQL function "foo" line 2 at SQL statement
LOCATION: DropErrorMsgNonExistent, utility.c:144

The error code is 42P01, which Appendix A shows as UNDEFINED TABLE.
The exception-handling block would therefore be:

BEGIN
DROP TABLE my_temp;
EXCEPTION
WHEN undefined_table THEN
NULL;
END;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/


That's exactly what I needed. I didn't think to set the verbosity to
get the error code. Thanks!
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
9
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
1
by: Robert McEuen | last post by:
Using Access 97 on WinXP I have data in a DB2 table that I'm trying to get into an identical table in my backend db. Based on volume of data and frequency of download, I'm trying to avoid...
9
by: BillCo | last post by:
I have a function which clears out a temporary table and then fills it with values from a recordset. One of the table fields is indexed with no duplicates. Very rarely - almost enough to ignore it...
1
by: Timothy Perrigo | last post by:
(PostgreSQL 8.0 beta 4 on Mac OS X 10.3.6) I'm working on a function which creates and populates a temporary table, then returns the number of records it has inserted. I'm getting an error,...
6
by: Ashok | last post by:
Hi, I am starting a new project to build a software product using APS.NET 2.0. In past I have used "frameset" and "frame" to build pages. My current requirements I have coded using frameset and...
5
by: theflyingminstrel | last post by:
Hi I have a question, it's probably very basic but I'm still learning the Javascript basics. The following form outputs a string change (from "input" to "text" box ). When the word "test" is...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
4
by: Stinky Pete | last post by:
Arvo, I've been reading about the above function, or similar, as it seems applicable to my problem, however I have to admit I am totally lost. What I need to do is update about 330 records that...
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.