?
Solved

Changing a Data Type from INT to BIGINT

Posted on 2013-02-04
3
Medium Priority
?
741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
deviprasadg earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

770 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