473,396 Members | 1,852 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,396 software developers and data experts.

checking existing before drop a table

Ken
how to check if a table is exist then drop it if true, false to ignore
the SQL statement, for example,

drop table person if exist;

Nov 12 '05 #1
10 9277
Why just drop the table? If the drop works, then you have what you
wanted. If the drop fails, then you have what you wanted anyway.

Nov 12 '05 #2
Ken
when fails, I always get a warning message, it is a little annoyed.

--CELKO-- wrote:
Why just drop the table? If the drop works, then you have what you
wanted. If the drop fails, then you have what you wanted anyway.


Nov 12 '05 #3
you can query syscat.tables

Nov 12 '05 #4
Ken wrote:
when fails, I always get a warning message, it is a little annoyed.

Actually that would we an error. In the CLP you can easily suppress the
error by calling
UPDATE COMMAND OPTIONS USING <magicyouneedtolookupininformationcenter> OFF
DROP TABLE ..
UPDATE COMMAND OPTIONS USING ... ON

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Serge Rielau wrote:
UPDATE COMMAND OPTIONS USING <magicyouneedtolookupininformationcenter> OFF


No magic needed.

UPDATE COMMAND OPTIONS USING o OFF;
DROP whatever you like;
UPDATE COMMAND OPTIONS USING o ON;

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #6
There is no way to do that directly, but a lot of things can be done to
do the same, depending on your needs.

If you are executing statements:

SELECT COUNT(*) FROM SysCat.Tables WHERE TabName = <Name>

And if the result is not 0, DROP TABLE ....

This can be a PROCEDURE as well:

CREATE PROCEDURE Drop_Table (Name VARCHAR(0128))
BEGIN
DECLARE A INTEGER;
SELECT COUNT(*) INTO A FROM SysCat.Tables WHERE TabName = Name;
IF A = 1 THEN EXECUTE IMMEDIATE "DROP TABLE " || <tabname>; END IF;
END

The only issue with the above, is that if someone else DROPs the TABLE
in between the two statements, such as in a concurrent process, this
will still produce the error. If you were DELETEing a row from a TABLE,
you could LOCK the TABLE to guarantee a static state, but there is no
equivalent LOCK SCHEMA that i know of.

Instead, the more "apropriate" way to do the above would be to DROP it
and simple catch the error with a DECLARE CONTINUE HANDLER FOR
SQLEXCEPTION, or more specifically, SQL0204N, and just ignore the
error.

B.

Nov 12 '05 #7
Brian Tkatch wrote:
There is no way to do that directly, but a lot of things can be done to
do the same, depending on your needs.

If you are executing statements:

SELECT COUNT(*) FROM SysCat.Tables WHERE TabName = <Name>

And if the result is not 0, DROP TABLE ....

This can be a PROCEDURE as well:

CREATE PROCEDURE Drop_Table (Name VARCHAR(0128))
BEGIN
DECLARE A INTEGER;
SELECT COUNT(*) INTO A FROM SysCat.Tables WHERE TabName = Name;
IF A = 1 THEN EXECUTE IMMEDIATE "DROP TABLE " || <tabname>; END IF;
END

The only issue with the above, is that if someone else DROPs the TABLE
in between the two statements, such as in a concurrent process, this
will still produce the error. If you were DELETEing a row from a TABLE,
you could LOCK the TABLE to guarantee a static state, but there is no
equivalent LOCK SCHEMA that i know of.


Well, that's what isolation levels are for to prevent. Given that a DROP
TABLE actually amounts to a DELETE on the catalog (aside from the physical
changes), you won't run into any problem with isolation RR.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #8
Please explain further, i do not understand.

Can you give me an example of setting the isolation level to allow the
DROP of a TABLE when a concurrent process DROPs it first?

B.

Nov 12 '05 #9
Brian Tkatch wrote:
Please explain further, i do not understand.

Can you give me an example of setting the isolation level to allow the
DROP of a TABLE when a concurrent process DROPs it first?


Just do this:

SET ISOLATION LEVEL TO RR

Then all operations are done in what the SQL standard calls "serializable",
i.e. no concurrency issues. So the transaction that checks for the
existence of the table via the SELECT COUNT(*) FROM syscat.tables acquires
read locks on the catalog, and another transaction cannot simply drop the
table, disregarding those read locks. That's just the usual locking stuff.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #10
Kewl thanx.

I just tried this. The DBA CREATEd a simple TABLE called 'A' for me. I
SET a default SCHEMA.

In two different connections:

db2 => set isolation level rr
DB20000I The SQL command completed successfully.
db2 => select count(*) from syscat.tables where tabname = 'A'

1
-----------
1

1 record(s) selected.

After both were like this, i issued a DROP TABLE A in both of them. The
first timed out due to the deadlock with a SQL0911N.

The second one, after the first errored, successfuly DROPped the TABLE.

After i COMMITted the second transaction, i did a DROP in the first
connection, which errored out with SQL0204N.

B.

Nov 12 '05 #11

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

Similar topics

3
by: Gunnar Vøyenli | last post by:
Hi (SQL Server 2000) I have an existing table (t) with a column that is NOT an identity column (t.ID), but it has manually inserted "row numbers". I want to make this column become an identity...
3
by: Bennett Haselton | last post by:
I'm working on an ASP.Net project where I want to test code on a local machine using a local database as a back-end, and then export it to the production machine where it uses the hosting...
7
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment...
1
by: JS | last post by:
I have set up db2audit for checking on win2k v8 db, but when I extract the records from the log and load them into the checking table, I don't get the object name in objname colum. My understanding...
2
by: Janning Vygen | last post by:
hi PGurus, i searched the archives and read the docs, because this problem shouldn't be new. But i really don't know what to search for. i am populating a database (v7.4.1) with COPY. Some...
2
by: Venkata Narayana | last post by:
Hi, You all may be knowing that Connection.isClosed() does not tells us if the underying DB connection is active or not; it only checks if Connection.close() had been previously called or not....
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
0
by: cvijaykrishna | last post by:
i am having a web based application and i am having a problem with it pls check it Explanationi am sending a sample code plese see it in VS-2005 FOR BETTER UNFERSTANDING I have a main page...
2
by: DavidPr | last post by:
I'm creating (trying to create) a picture gallery for my website. The script is not working. I've been working on it now for about 80 hours with no success. My php skills aren't very good. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.