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

New to DB2: Create table if exists? How?

Hi all,

I'm new in using DB2 and I have question on creating/ altering table.

I created a file (DDL) with SQL statements, then the database
administrator (my client) to create or alter the table automatically
by running the command "db2 -tvf <filename>".

However, now, I would like to create the table if it doesn't exist. Or
Alter the table if it exists.

I found some posts mentioned the statement of:
IF NOT EXISTS (SELECT NAME FROM SYSIBM.SYSTABLES WHERE NAME="MYTABLE")
THEN
CREATE TABLE MYTABLE (......)
END IF

I tried to add such statements in the file, but it failed.

So, how can I do this?

Please help & thanks.
Vincent Ho

Oct 2 '07 #1
1 14812
vi********@gmail.com wrote:
Hi all,

I'm new in using DB2 and I have question on creating/ altering table.

I created a file (DDL) with SQL statements, then the database
administrator (my client) to create or alter the table automatically
by running the command "db2 -tvf <filename>".

However, now, I would like to create the table if it doesn't exist. Or
Alter the table if it exists.

I found some posts mentioned the statement of:
IF NOT EXISTS (SELECT NAME FROM SYSIBM.SYSTABLES WHERE NAME="MYTABLE")
SQL uses single-quotes to delimit strings: NAME = 'MYTABLE'
THEN
CREATE TABLE MYTABLE (......)
You have to use dynamic SQL for that, i.e. create a string that contains the
CREATE TABLE statement, then use EXECUTE IMMEDIATE to execute the statement
in that string.
END IF
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Oct 2 '07 #2

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

Similar topics

3
by: Jagdip Singh | last post by:
Hi, We are facing problem accessing to DB2. Seems like database manager is down I tried starting it using db2start but it was neither showing any messages nor returning to command prompt...
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
12
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
2
by: adri4n | last post by:
as wat ive mentioned in the title.. im would like to know whether the a particular record/table is being locked in my program. some of the methods which i would like to develop are as below: ...
3
by: vj_dba | last post by:
Hi Group, I have a Primary key in my table. It's clear Primary key wont allow duplicates, this primary key creates one index for retrival. Suppose if my table is having a Unique index also....
5
by: Sphenix | last post by:
------------------------ UPDATE A SET A.ID = '?' + A.ID FROM TABLEA A LEFT OUTER JOIN TABLEB B ON A.INDEX = B.INDEX WHERE B.DUP_ID IS NULL ------------------------ seems like update with...
9
by: mitek | last post by:
Hi, All. Is federated procedures between DB2 databases currently not supported in WebSphere Federation Server 9.1 ??? I recieve next error when trying to create DB2 federated procedure : ...
0
by: aashiss03 | last post by:
hi everyone I m restorimg a database , during which i m facing the peoblem Here is the details description the problem. RESTORE DATABASE NSWD FROM "C:\db2_backups" TAKEN AT...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.