Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Changing formula for a computed column


Changing formula for a computed column

Author
Message
viduvs
viduvs
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 284
Hi,
Can we change the formula of an exisiting computed column in a table using T-SQL?
I know it can be done using SQL Server Mgmt Studio UI but I want to do it through query.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14931 Visits: 38917
i believe the calculated column has to be dropped and recreated as a new calculated column.
i just tried this as a proof of concept, and I get a syntax error on the ALTER command:



create table Example(exampleID int identity(1,1) not null primary key,
exampleInt int,
exampletext varchar(30),
myCalculatedColumn as exampleInt * 2 )
--syntax error, so this format is incorrect.
alter table Example Alter Column myCalculatedColumn as (exampleInt * 2 + 10)



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Rob Schripsema
Rob Schripsema
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 11042
Yes, you have to drop old column and add a new one.

ALTER TABLE dbo.MyTable
   DROP COLUMN OldComputedColumn

ALTER TABLE dbo.MyTable
ADD NewComputedColumn AS OtherColumn + 10



In case you weren't yet aware, you can get SSMS to write queries for you. Use the UI to set up the change you want to make, but before you click the Save button (or close the Design window), right click on the design surface and select "Generate Change Script". It will give you a script that you can cut and paste into a query window. It doesn't always give you the most efficient script or even the most efficient method for performing the task, but it will get you started. And it's a good way to get more familiar with (or refresh your memory of) DDL syntax.

Rob Schripsema
Propack, Inc.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search