Modify Computed Columns Using T-SQL
Hi,
I have a table with 4 columns let us say A,B,C,D.
column D is computed column with formula A + '-' + B
Now, i want to add one more condition to the formula which looks like "
A + '-' + B + '-' + C".

Please let me know how to do this using T-SQL as i cannot open the table in design mode in production server.

Thanks in Advance!!

October 8th, 2007 7:25pm

First remove the current computed column.

ALTER TABLE MyTestTable
DROP COLUMN MycomputedColumn

Then add the new computed column (with same name.)

ALTER TABLE MyTestTable
ADD MyComputedColumn AS ( A + '-' + B + '-' + C )

Free Windows Admin Tool Kit Click here and download it now
October 8th, 2007 8:04pm

Hi,
Thanks for the repsonse!!
Instead of dropping and recreating the column, can we modify the formula using T-SQL?
Recreating column affects the column order .

Thanks

October 8th, 2007 8:35pm

Unfortunately, in order to change a computed column, you must DROP and re-CREATE the column.

Actual Table Column order is inconsequential.

All Best Practice documents highly encourage, and experienced DBAs will always list the columns wanted, in the presentation order desired, in the Query. Applications and users 'should' not rely upon a specific column order unless specifically requested in the query.

You 'should' never (or at least, rarely) use [ SELECT * ]

The ANSI SQL standards require that column order be enforced only with an [ ORDER BY ] clause.
Free Windows Admin Tool Kit Click here and download it now
October 8th, 2007 9:02pm

Arnie,
Thanks for the reply!!
I agree with you that we should use column names in the query instead of select *.

but i think, alter column should be available to modify the formula in computed column specifcation atleast in the next version.
:-)

Thanks

October 8th, 2007 9:12pm

Thanks Bob,

You 'might' wish to 'weigh in' and vote on this product enhancement request.

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=124781

(Click on the rating star that you deem appropriate.)

Free Windows Admin Tool Kit Click here and download it now
October 8th, 2007 9:46pm

ALTER TABLE MyTestTable
   ADD MyComputedColumn AS ( A + '-' + B + '-' + C )

This query is not working on Ms-Access.

Can I have query for that.

August 11th, 2014 7:21am

ALTER TABLE MyTestTable
   ADD MyComputedColumn AS ( A + '-' + B + '-' + C )

This query is not working on Ms-Access.

Can I have query for that.

Definitely, it wont work in MS Access. You may put your question in Access forums.

http://www.accessforums.net/

Free Windows Admin Tool Kit Click here and download it now
August 11th, 2014 7:32am

And some additional suggestions:

  • Don't post your own follow up question to an old and answered thead.  Many people will simply ignore those threads because they are marked as "answered".
  • Choose the most appropriate forum for your question since that affects who will see it.
  • Writing that something "doesn't work" is not helpful - EVER.  No one can see what you are doing or how you are doing it.  No one can see your screen and the error / warning messages that are displayed on it. Help your readers help you by posting your question with sufficient information to understand what you are doing and what you are trying to accomplish. 
August 11th, 2014 1:39pm

is there any other way to change formula of a computed column? i tried dropping the column but i took an error because the column is related and it has data in it. 

i can create but i can't change :(

thank you

Free Windows Admin Tool Kit Click here and download it now
March 13th, 2015 10:45am

Please create a new thread for your question. This one is old and answered.
March 13th, 2015 11:39am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics