Adding Computed Column to an Existing Table

Posted on 2009-05-26
Medium Priority
Last Modified: 2012-05-07
I have this table and need the FacilityID to be a computed column; yes I can drop and recreate but having insert issues with the computed column in place.  This table has all the data now but I need to change the FacilityID to the computed column...

FacilityID as MainID


CREATE TABLE [dbo].[tbl_Facility](
      [FacilityID] [int] NULL,
      [MainID] [int] IDENTITY(1,1) NOT NULL,
      [FACID] [int] NULL,
      [BldID] [int] NULL,
      [BldGrpID] [int] NULL,
      [RoomID] [int] NULL,
      [Facility_Name] [varchar](max) NULL,
      [Facility_Description] [varchar](max) NULL,
      [CenterID] [int] NULL,
      [Building_Nos] [varchar](max) NULL,
      [Rooms] [varchar](max) NULL,
      [Capability] [nvarchar](max) NULL,
Question by:Glen_D
  • 2
LVL 60

Expert Comment

ID: 24477201
alter table tbl_Facility
add FacilityID as [MainID]
LVL 39

Accepted Solution

BrandonGalderisi earned 2000 total points
ID: 24477202
If you convert facilityID to a computed column, it will fail any inserts that specify that field in the insert list.

If you are ok with that.

alter table tbl_facility drop column facilityId
alter table tbl_Facility add FacilityId as MainId

Your statement doesn't define your primary key.  You cannot have a PK on a computed column.
LVL 60

Expert Comment

ID: 24477226
whoops...brandon is right, you'll need to drop the column first.
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24477238
alter table [dbo].[tbl_Facility] drop column [FacilityID]
ALTER TABLE dbo.tbl_Facility ADD FacilityID  AS mainid

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

600 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