Solved

Changing a Data Type from INT to BIGINT

Posted on 2013-02-04
3
634 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

708 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

15 Experts available now in Live!

Get 1:1 Help Now