SQL Server Management Studio Error : Script failed for UserDefinedFunction

别说谁变了你拦得住时间么 提交于 2019-12-12 09:46:53

问题


I'm working on a local and a remote SQL Server instance with SSMS. I create a tiny function like:

create function ufnTestFunc ()
returns int
begin
    return 1
end

When I try to 'modify' it, or choose 'script function as -> alter', I get an error like:

Script failed for UserDefinedFunction 'dbo.ufnTestFunc'. (Microsoft.SqlServer.Smo)

- Syntax error in TextHeader of UserDefinedFunction 'ufnTestFunc'. (Microsoft.SqlServer.Smo)

This also happens on already existing functions. What may be the reason?


Notes:

  • All the functions work as intended
  • I can script the function as 'create' with no problem.
  • It's not related with comments, as there are no comments in the test function
  • Same happens with different DBs on the remote server

Local Server:
Microsoft SQL Server Express Edition (64-bit) - 10.50.2500.0

Remote Server:
Microsoft SQL Server Web Edition (64-bit) - 10.50.1600.1

SSMS:

Microsoft SQL Server Management Studio    10.50.2500.0
Microsoft Data Access Components (MDAC)   6.1.7601.17514
Microsoft MSXML                           3.0 6.0 
Microsoft .NET Framework                  2.0.50727.5448

回答1:


Run sp_helptext N'ufnTestFunc'

Is there anything in the "comment" section above the function that looks odd?

Embedded comments /* */ can sometimes cause that error.

Example:

/* 
    This function does something. 
    /* NOTE: not any useful, though */
    More stuff...
*/
alter function ufnTestFunc ()
returns int
begin
    return 1
end



回答2:


Ok like someone mentioned before, but with a little difference.

The problem is the AS keyword. Although the AS keyword before the function content is optional, SSMS can't handle functions without it. They work, but make trouble. It's a bug and not mentioned anywhere in the BOL.

I normally use the AS keyword, but this time the DB is from a previous coder, who didn't use it. In my test function I also didn't use it to make the function as small as possible.




回答3:


--Adding that I received this error and could neither script nor modify the sproc.

The problem was a block comment section inline, inside another block comment.

/*  
Example call:  
 Exec [usp_mySproc] @Param = '172777'  /* blah, blah */
 Exec [usp_mySproc] @Param = '172777', @Debug = 1   WITH RECOMPILE   
*/ 

Removed inner comment block and no problem.



来源:https://stackoverflow.com/questions/8731038/sql-server-management-studio-error-script-failed-for-userdefinedfunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!