November 21, 2012 at 9:57 pm
Hi
We need to change int column to bigint data type on a primary key column.
So we generate script from managment studio. it changed data type by using temp table.
My question why they did not use alter column query to change data type? which is the best alter column method or temp table method ? please provide your suggestion.
Thanks
Vel
November 21, 2012 at 10:39 pm
going with alter column method is best option according to me as its a primary column for your table.
November 21, 2012 at 10:41 pm
First ,thanks for the nice question ,I always used to thought it takes alter table command at the back end .
I have attached my results with client statistics .
Waiting for experts to comment on it .
-----------------------------------------------------------------------------
संकेत कोकणे
November 21, 2012 at 10:50 pm
sanket kokane (11/21/2012)
First ,thanks for the nice question ,I always used to thought it takes alter table command at the back end .I have attached my results with client statistics .
Waiting for experts to comment on it .
Now check the before'n'after fragmentation of the clustered index and see what happens. I haven't tried it but I'm thinking it will have gotten worse.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2012 at 12:02 am
The test result i post,was done on table without primary key .
in OP's case,he want it to alter column which having primary Key .
if you Use
alter table tablename
alter column columnname bigint
it gives error
Msg 5074, Level 16, State 1, Line 2
The index 'PK__tablename __05AD357414270015' is dependent on column 'columnname'.
Msg 4922, Level 16, State 9, Line 2
ALTER TABLE ALTER COLUMN columnname failed because one or more objects access this column.
So temp_table is the only option .
-----------------------------------------------------------------------------
संकेत कोकणे
November 22, 2012 at 12:14 am
No, please try this
alter table Atest drop constriant PK_ATest
alter table Atest alter column id bigint
alter table Atest add constriant PK_Atest primary key clustered (id)
November 22, 2012 at 12:44 am
k.thanigaivel (11/22/2012)
No, please try thisalter table Atest drop constriant PK_ATest
alter table Atest alter column id bigint
alter table Atest add constriant PK_Atest primary key clustered (id)
that isn't like you are logically creating new table ?
-----------------------------------------------------------------------------
संकेत कोकणे
November 22, 2012 at 1:40 am
sanket kokane (11/22/2012)
k.thanigaivel (11/22/2012)
No, please try thisalter table Atest drop constriant PK_ATest
alter table Atest alter column id bigint
alter table Atest add constriant PK_Atest primary key clustered (id)
that isn't like you are logically creating new table ?
Into object dependencies here, as the PK index is tied to the column you simply just cant change the column, you need to remove the index, change the column, re-add the index. So you need to make the table a heap, then change it back to a B-Tree when changing a clustered index.
Also one thing that no one has mentioned yet is any foreign keys which reference the column as well will need to be dropped all referenced columns changes and all FK's recreated.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply