If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Alter a table add column if not exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-25-10, 06:07
PRASATH.A PRASATH.A is offline
Registered User
 
Join Date: May 2010
Posts: 4
Alter a table add column if not exists

hi ,

this is prasath,

i am new to db2,

i am in need to ALTER TABLE ADD COLUMN IF THE COLUMN DOES NOT EXISTS<

is it any solutions for this ?

if any possible solutions please reply me

Thanks and Regards

Prasath A
Reply With Quote
  #2 (permalink)  
Old 05-25-10, 08:11
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If you try to add the column and it already exists, then the add will fail. If you try to add it and it does not already exists, it will succeed. You can do this one of two ways. You can check if the columns already exists by querying syscat.columns, or you just try to add the column and handle the error if it already exists.

Andy
Reply With Quote
  #3 (permalink)  
Old 05-25-10, 08:16
PRASATH.A PRASATH.A is offline
Registered User
 
Join Date: May 2010
Posts: 4
Thank You ANDY

CREATE PROCEDURE PR1
BEGIN
IF NOT EXISTS(
select * from SYSIBM.syscolumns where tbname='S_2' and name='NAME' ;
)THEN

ALTER TABLE S_2 ADD S_2_FIRSTNAME VARCHAR(250);
END IF;
END

actualy am using the above procedure to get do the stuff.

but the procedure is not created shows some error,

i have get the code to find the column is already exists or not

but the procedure is not created ,

can u please reply , what could be the issue

thanks and regards

Prasath A
Reply With Quote
  #4 (permalink)  
Old 05-25-10, 08:21
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
How would I know what the problem is? You did not supply the error you are getting.

Andy
Reply With Quote
  #5 (permalink)  
Old 05-25-10, 08:58
PRASATH.A PRASATH.A is offline
Registered User
 
Join Date: May 2010
Posts: 4
hi andy

can u able to give me the code for alter the table add a column if the does not exists,if exists dont add.

thanks and regards

Prasath a
Reply With Quote
  #6 (permalink)  
Old 05-27-10, 10:10
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
First, you must include a schema name in your query or you may not check for the right table. Then you may have to use dynamic SQL instead of static SQL for the ALTER TABLE statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 05-28-10, 00:34
PRASATH.A PRASATH.A is offline
Registered User
 
Join Date: May 2010
Posts: 4
Thank you stolze

Thank you stolze
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On