Alter scalar function in SQL server 2008 that referring in the computed column of a table -


i have created scalar function in sql server 2008 , same referring in computed column in few of tables. want alter function without dropping table. throws error:

cannot alter 'dbo.getstatus' because being referenced object 'order'.

is possible alter function? or drop , create dependable table first , alter function?

here function:

create function [dbo].[getstatus] (     @fromdate datetime,     @todate datetime ) returns tinyint begin      declare @ret tinyint;     if(@fromdate<=getdate() , (@todate>=getdate() or @todate null))         set @ret= 1     else         set @ret= 0     return @ret end 

and referring in table:

create table [dbo].[order](     [id] [int] identity(1,1) not null,     [name] [varchar](200) not null,     [effectivefromdate] [datetime] not null,     [effectivetodate] [datetime] null,     [status]  ([dbo].[getstatus]([effectivefromdate],[effectivetodate])) ) 

this design. should first drop defaults/constraints, alter function , add constraints back. no need drop tables.

but can work around following trick:

add intermediate function call actual function; alter computed columns call intermediate function instead of actual.

example:

create function dbo.fnactual ( @p int ) returns int     begin         return @p + 1     end go   create function dbo.fnintermediate ( @p int ) returns int     begin         return dbo.fnactual(@p)     end go   create table testtable(id int, fn dbo.fnintermediate(id)) go 

insert value:

insert dbo.testtable values  ( 1 )  select * dbo.testtable --selects 2  --throws exception alter function dbo.fnintermediate ( @p int ) returns int     begin         return dbo.fnactual(@p)     end go   --succseeds alter function dbo.fnactual ( @p int ) returns int     begin         return @p + 2     end go  select * dbo.testtable --selects 3 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -