views:

1375

answers:

4

If you set up a table's column to be a computed column whose Formula calls a Function, it becomes a pain to change that underlying Function. With every change, you have to find every single column whose Formula that references the Function, remove the reference, save the Table, alter the Function, add everything back, and save again. Even small changes are nightmares.

Can you tell SQL Server that you don't care that the Function is being referenced by Formulas and to just go ahead and change the underlying Function?

Additional Details: The computed column is not persisted or referenced by a FK constraint because it is non-deterministic. The function takes into consideration the current time. It's dealing with the question of whether a record is expired or not.

+2  A: 

No, as far as I know, you cannot do this - you'll have to first remove all computed columns referencing a function, alter the function, and then recreate the computed columns.

Maybe MS will give us a "CREATE OR ALTER FUNCTION" command in SQL Server 2010/2011? :-)

Marc

marc_s
Well, I suppose you just can't do this. And that's what you said and you said it first.
colithium
A: 

You could try with some good schema compare tool, that create the script for you :)

I have SQL Compare 8 and damn if I can find a menu command to do this...
jcollum
+2  A: 

The consequences of the ALTER could be huge.

Have you indexed the columns? Used it in a view with schemabinding? Persisted it? Foreign key relationship to it?

What if the ALTER changes the datatype, NULLability or determinism?

It's easier to stop ALTER FUNCTION with dependencies than deal with so many scenarios.

gbn
The function is time dependent and can't be persisted. For that same reason it can't have a FK constraint (even though the column ALWAYS happens to be a valid value referencing another table's PK).
colithium
This is *your* function, not the general case. What's better: consistently disallow ALTER FUNCTION with dependencies or allow it pretty much randomly?
gbn
My function is not anything special, it's behavior comes from it being non-deterministic. I clarified that in my question with an edit. There should be no ill-effects caused by changing out non-deterministic functions. But is it possible?
colithium
Unfortunately, no.
gbn
A: 

You could change the column to be not-computed, and update it by TRIGGER.

Or you could rename the table to something else, drop the computed column, and create a VIEW in place of the original table (i.e. with the original table name) and including the "computed" column you need.

EDIT: note that this may mess with your INSERTs into the original table name (now a VIEW). Obviously you could keep the old table, drop the computed column, and create a separate VIEW that contained the computed column.

We've had to work around Computed Columns enough times to have decided they are more trouble than they gain. Fail-saf inserts(1), trying to insert into VIEWs onto tables with computed columns, things that require messing with SET ARITHABORT and so on.

(1) We have fail-safe inserts like:

INSERT INTO MyTable SELECT * FROM MyOtherTable WHERE ...

which are designed to fail if a new column is added one table and not the other. With Computed Column we have to explicitly name all columns, which loses us that safety net.

Kristen
That's the thing, the value of the computed column is non-deterministic. Meaning if you ask it what it is it could say one thing and then one second later with absolutely no data changes, it could say something different. Triggers won't work here.
colithium
I did read that said is was non-deterministic, my mind then wandered off! Sorry about that.
Kristen