Solved

Changing a Data Type from INT to BIGINT

Posted on 2013-02-04
3
648 Views
Last Modified: 2013-03-27
I have a PK column which was created as a INT IDENTITY(1,1) NOT FOR REPLICATION NOT NULL - I need to change this columns DataType to BIGINT

Can I just drop the primary key constraint and then

--ALTER TABLE [TableName]
--ALTER COLUMN  ColumnName  BIGINT NOT NULL

If I do this will it keep the same structure with the IDENTITY field and the not for replication not null or do I need to do something different

Thanks In Advance
0
Comment
Question by:VitaminD
3 Comments
 
LVL 10

Accepted Solution

by:
deviprasadg earned 500 total points
ID: 38853868
It should keep the same structure with the IDENTITY field and the not for replication not null.
Your plan looks good.

Below is the demo script:
DROP TABLE TESTPK
GO

CREATE TABLE TESTPK(
ID INT IDENTITY(1,1) NOT FOR REPLICATION NOT NULL ,
A VARCHAR(100))
GO

ALTER TABLE TESTPK ADD CONSTRAINT PKTESTPK PRIMARY KEY (ID)
GO

INSERT INTO TESTPK(A)
SELECT 'A'
GO 10

SELECT * FROM TESTPK
GO

ALTER TABLE TESTPK
DROP CONSTRAINT PKTESTPK

GO

ALTER TABLE TESTPK
ALTER COLUMN ID BIGINT

GO

ALTER TABLE TESTPK ADD CONSTRAINT PKTESTPK PRIMARY KEY (ID)

GO

INSERT INTO TESTPK(A)
SELECT 'A'
GO 10

SELECT * FROM TESTPK
GO

Open in new window

0
 
LVL 4

Expert Comment

by:krtyknmsql
ID: 38853926
Yes, You can drop the Primary Key constraint then alter the column to change the data type and recreate the primary key constraint as Deviprasad mentioned with three steps.

ALTER TABLE Tbl DROP CONSTRAINT PK_Tbl
GO

ALTER TABLE Tbl ALTER COLUMN ID BIGINT 
GO

ALTER TABLE Tbl ADD CONSTRAINT PK_Tbl PRIMARY KEY (ID)
GO

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38854257
which version/edition of sql server?
how big is the table rows/columns?
is the column involved in any other parent/child relations ...? (ie is it used as a foreign key in any other tables?)

is the table actually being published (or is it the destination of a publication) as part of a replication strategy?

...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now