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 > Altering a Table Column with data from not null to null

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-22-06, 11:28
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Altering a Table Column with data from not null to null

Hi,

Can anyone please tell me the best,simple and fastest method to change a table column which has data in it from not null to null in DB2.

I have worked on it by exporting ,dropping recreating and importing. But i feel there is another simple way to do it.

Thanks,
Venky
Reply With Quote
  #2 (permalink)  
Old 05-22-06, 15:11
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Any chance you know the DB2 Version and platform ..

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 05-22-06, 15:22
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Thanks for replying to my post as I have seen people viewing it but no replies.
The version I am working on is DB2/AIX64 8.2.2.
Reply With Quote
  #4 (permalink)  
Old 05-23-06, 23:48
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
There is no better way when you have to stick to DB2. Even next version of DB2 does not let you alter the column directly to NULL.
Reply With Quote
  #5 (permalink)  
Old 05-24-06, 09:23
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Thanks for the reply. I just wanted to clear my doubt. I thought DB2 allows you to make it null from not null but not Null to Not Null.

Venky
Reply With Quote
  #6 (permalink)  
Old 05-25-06, 02:09
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Cool

If you are on Version 8.2 you can try the following: It worked for me,

Step1> In this table COL1 isnot null.
db2 "describe table tb_test1"

Column Type Type
name schema name Length Scale Nulls

------------------------------ --------- ------------------ -------- ----- -COL1 SYSIBM SMALLINT 2 0 No

COL2 SYSIBM SMALLINT 2 0 Yes

You have some records loaded in this table.

Step2> Run the following Stored Proc:

db2 "CALL SYSPROC.ALTOBJ ( 'APPLY_CONTINUE_ON_ERROR', 'CREATE TABLE SCHEM1.TB_TEST1 ( COL1 SMALLINT , col2 smallint ) IN USERSPACE1 PARTITIONING KEY ( COL1) ', -1, ? )"

(Note:If you have two not null cols which you want to make null then (COL1,COL2)',-1.....)

Step3>
See your desired result.

db2 "describe table tb_test1"

Column Type Type
name schema name Length Scale Nulls

------------------------------ --------- ------------------ -------- ----- -COL1 SYSIBM SMALLINT 2 0 Yes

COL2 SYSIBM SMALLINT 2 0 Yes


Thanks,
Jay
New Delhi, India
Reply With Quote
  #7 (permalink)  
Old 05-25-06, 10:46
venky5436 venky5436 is offline
Registered User
 
Join Date: Jul 2005
Posts: 47
Hi Jayan,

Thanks for the solution ,looks like this will be helpful to many people.Will try this SP and check out.
Reply With Quote
  #8 (permalink)  
Old 05-26-06, 00:08
DBA-Jr DBA-Jr is offline
Registered User
 
Join Date: Mar 2005
Posts: 108
Hi Jay,
This SP is very helpful. Could you please share a little more, where is this SP documented? Can you post a link please? I would like to learn about some other SP's, too.

Thanks,
Gary
Reply With Quote
  #9 (permalink)  
Old 05-26-06, 02:06
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Hi,

You can see the list of all the available procs by running the following. Regarding the arguments you need to pass you can search the same at IBM Developers works:

db2 "select PROCNAME from syscat.procedures where PROCSCHEMA = 'SYSPROC' "

Thanks,
Jayanta Datta
New Delhi, India
Reply With Quote
  #10 (permalink)  
Old 05-26-06, 02:07
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Hi,

You can see the list of all such available system procs by running the following. Regarding the arguments you need to pass you can search the same at IBM Developers works:

db2 "select PROCNAME from syscat.procedures where PROCSCHEMA = 'SYSPROC' "

Thanks,
Jayanta Datta
New Delhi, India
Reply With Quote
  #11 (permalink)  
Old 05-31-06, 03:11
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
is there any way to do it opposite: from 'null' to 'not null'?
Thanks,
Grofaty
Reply With Quote
  #12 (permalink)  
Old 05-31-06, 03:16
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

I have figured out:

CALL SYSPROC.ALTOBJ ('APPLY_CONTINUE_ON_ERROR', 'CREATE TABLE SCHEM1.TB_TEST1 (COL1 SMALLINT NOT NULL, COL2 SMALLINT NOT NULL) IN USERSPACE1 PARTITIONING KEY (COL1)', -1, ? );

This is excelent news!!!
Grofaty
Reply With Quote
  #13 (permalink)  
Old 05-31-06, 03:25
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
this also works:

CALL SYSPROC.ALTOBJ ('APPLY_CONTINUE_ON_ERROR', 'CREATE TABLE SCHEM1.TB_TEST1 (COL1 SMALLINT NOT NULL, COL2 SMALLINT NOT NULL) IN USERSPACE1 PARTITIONING KEY (COL1,COL2)', -1, ? );

and this also works:

CALL SYSPROC.ALTOBJ ('APPLY_CONTINUE_ON_ERROR', 'CREATE TABLE SCHEM1.TB_TEST1 (COL1 SMALLINT NOT NULL, COL2 SMALLINT NOT NULL)', -1, ? );

Why is PARTITIONING KEY cause important?

Thanks,
Grofaty
Reply With Quote
  #14 (permalink)  
Old 05-31-06, 04:05
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
it is interesting if primary key is created on table (but I am not changing the primary key columns definitions) the the following error is returned:
Code:
SQL0443N  Routine "ALTOBJ" (specific name "") has returned an error SQLSTATE
with diagnostic text "SQL1227  Reason code or token: 5|AVGCOLLEN|8
".  SQLSTATE=38553
... but table definitions are changed anyway. What does this error means?

My system: db2 v8.1 fixpack 9 (which is equal to v8.2.2) on Windows.

Thanks,
Grofaty

Last edited by grofaty; 05-31-06 at 04:25.
Reply With Quote
  #15 (permalink)  
Old 05-31-06, 05:18
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
I have found out that some statistics are changed before and after the SP if runstat is run.

I did:
1. runstats on table SCHEM1.TB_TEST1 with distribution and detailed indexes all
2. select * from sysibm.sysindexes where tbname='TB_TEST1'
3. execute SP
4. repeat step 1
5. repeat step 2

Statistic from step 2 and step 5 are changed. Why? Is that the reason of error code posted in previous post?

Thanks,
Grofaty

Last edited by grofaty; 05-31-06 at 05:22.
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