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

4

75

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!

Homestead answered 3/3, 2010 at 15:44 Comment(1)
What is not working? Are you getting an error? Is it not doing what you are expecting? If so, what are you expecting?Hamartia
B
84

If you're trying to change an existing column, you can't use ADD. Instead, try this:

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))

EDIT: The above is incorrect. When altering a computed column the only thing you can do is drop it and re-add it.

Bailar answered 3/3, 2010 at 15:49 Comment(0)
W
108

Something like this:

ALTER TABLE dbo.MyTable
DROP COLUMN OldComputedColumn

ALTER TABLE dbo.MyTable
ADD OldComputedColumn AS OtherColumn + 10

Source

Wolf answered 1/10, 2012 at 19:52 Comment(1)
What if you have a computed column in the middle of all columns? Won't this DROP + ADD cause the column to be added at the end of the table (and edit all other columns' ORDINAL position?)Cominform
B
84

If you're trying to change an existing column, you can't use ADD. Instead, try this:

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))

EDIT: The above is incorrect. When altering a computed column the only thing you can do is drop it and re-add it.

Bailar answered 3/3, 2010 at 15:49 Comment(0)
D
5

This is one of those situations where it can be easier and faster to just use the diagram feature of SQL Server Management Studio.

  1. Create a new diagram, add your table, and choose to show the formula column in the diagram's table view.
  2. Change the columns formula to an empty string ('') or something equally innocuous (probably such that you don't change the column's datatype).
  3. Save the diagram (which should save the table).
  4. Alter your function.
  5. Put the function back in the formula for that column.
  6. Save once again.

Doing it this way in SSMS will retain the ordering of the columns in your table, which a simple drop...add will not guarantee. This may be important to some.

Droshky answered 24/3, 2016 at 19:51 Comment(2)
This is a useful solution. I want to qualify that when working with very large tables, millions to billions of rows, this will cause a table rebuild. This could take HOURs to perform, depending on the width of the table and types of data stored (MAX, for example). In order to avoid a table rebuild, it's necessary to drop and add; with the premise that all SQL queries to the table are not done with queries that exact field order is specific (ie. INSERT INTO, without field names)Hume
@Hume - something very similar happened at my company because they had a bad setting in their deploy add adding a column in the table definition not at the end cause a full rebuild. The important thing here is that you should NEVER care about column ordinality. If you want to display in a certain order, then modify your select. You should NEVER be using SELECT * anyway. This answer is supporting bad development habits (although it is technically correct)Bostwick
B
1

Another thing that might be helpful to someone is how to modify a function that's a calculated column in a table (Following query is for SQL):

ALTER <table>
DROP COLUMN <column>

ALTER FUNCTION <function>
(
<parameters>
)
RETURNS <type>
BEGIN
...
END

ALTER <table>
ADD <column> as dbo.<function>(parameters)

Notes:

  1. Parameters can be other columns from the table

  2. You may not be able to run all these queries at once, I had trouble with this. Run them one at a time

  3. SQL automatically populates calculated columns, so dropping and adding won't affect data (I was unaware of this)
Bundle answered 15/11, 2019 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.