Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Changing PK column type
Message
 
 
To
14/01/2016 05:39:07
Dragan Nedeljkovich
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01629774
Message ID:
01629789
Views:
31
>>Hi,
>>
>>I need to change the type of a PK column of a table from type NUMERIC (6,0) to INT (When I created this table I never thought that the number of records would grow to be more than 999,999 but now I need more).
>>
>>When I change the type in the SSMS it works, but it takes quite a bit of time on a table of about 600,000 rows. I thought that changing the type using the script would be faster. And I tried the following script:
>>
>>ALTER TABLE dbo.MyTable
>>ALTER COLUMN wo_number INT 
>>
>>
>>But I get the following errors:
>>
>>he object 'PK_MyTable' is dependent on column 'wo_number'.
>>Msg 5074, Level 16, State 1, Line 1
>>The index 'open_wo' is dependent on column 'wo_number'.
>>
>>That is, the PK index and another index do not allow the script to change the type. Does it mean that the only way to change the type of this PK is via SSMS?
>
>SSMS is just a client. I guess you could write something just as half-usable :).
>
>The actual source of the problem are the restrictions within the database itself; the PK is a record somewhere in the metadata (master database, to be specific) and there's a relational integrity check which is kicking in and you simply can't change the PK field without deleting the PK index first.
>
>The easiest way is to have your client (SSMS, which I don't recommend but one uses what one has) build a script to create the table, then you edit the script to have the PK as integer, then rename the original table, run the script to create the table, insert into newtable select * from oldtable, done.
>
>The trouble here is that you need to find out where in SSMS all the scripting options are, and to set them on - by default they are off, as it's Microsofts time honored practice to have most of the defaults wrong. The script may also need some manual tweaking, depending on which features your table is using. But you can try things out - can create the same empty table over and over until you're satisfied with the result.

Thank you for the explanation. Of course I don't have the skill no the time to create anything like SSMS. So I will use it to change the type. I only have a handful of databases where the number of the rows is very high (over 600,000).
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform