Today, when I tried to Alter a function in my database, I got the error info in the title. Then I tried some example function operations, and found the reason: Inline table-value function and Multi-Statement table-valued function are actually different object type, and you can never do Alter one object to other types(If I am wrong here, please tell me).
Now you could try it yourself:
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type--First, we create a inline-table function which returns a table
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
Create Function dbo.yukunFunctionTest (@param int@param1 int)
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
returns table
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
AS
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
RETURN
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type(
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type    
select 1 as id
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type)
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
--Then, we want to alter it to a multi-statement table-valued function,
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type--
and we will get an error here
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
alter Function dbo.yukunFunctionTest (@param int@param1 int)
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
returns @tt table
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type(
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type    id 
int
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
begin
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type    
insert into @tt values (1)
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type    
return
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
end

  By the way, you can get the type value and type desc of any objects in your database from table sys.objects.
  And, which is very important, if  you really want to "alter" the type of your function, actually you need to drop it first, then create a new one. When you do it, pay attention to the permissions to the dropped objects, because you will lose all the permission info when you drop it, and you need to restore them after you create the new one.Bellow is a demo scripts which could do this for you:

Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object typeselect @grantee varchar(200)
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
select @grantee=name
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
from syspermissions p
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
join sysusers u on p.grantee = u.uid
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
where id = object_id('ObjectName')
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
Cannot perform alter on 'dbo.ObjectName' because it is an incompatible object type
grant select on dbo.ObjectName to @grantee

 

 

相关文章: