Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alter this computed column in SQL Server 2008?

I have a computed column created with the following line:

alter table tbPedidos  add restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 then 1 else 0 end as bit)) 

But, now I need to change this column for something like:

alter table tbPedidos  alter column restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 or restricaoValor = 1 then 1 else 0 end as bit)) 

But it's not working. I'm trying to input another condition to the case statement, but it's not working.

Thanks a lot!

like image 204
André Miranda Avatar asked Mar 03 '10 15:03

André Miranda


People also ask

Can we alter computed column in SQL Server?

A: There is NO way to alter computed column. You will have to drop and recreate it.

How can I edit a computed column in SQL Server?

Answers. First remove the current computed column. Then add the new computed column (with same name.) Unfortunately, in order to change a computed column, you must DROP and re-CREATE the column.

How do you assign a name to a computed field in SQL?

How do you assign aname to a computed field? It is a field whose values you can derive from existing fields. By putting fieldName (operation:+,-,*,/) fieldName. You assign names by putting the word AS after the computation then puttingthe name you want to call it.


1 Answers

Something like this:

ALTER TABLE dbo.MyTable DROP COLUMN OldComputedColumn  ALTER TABLE dbo.MyTable ADD OldComputedColumn AS OtherColumn + 10 

Source

like image 102
Leniel Maccaferri Avatar answered Sep 22 '22 13:09

Leniel Maccaferri