Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Management Studio Error : Script failed for UserDefinedFunction

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
like image 677
SuperDuck Avatar asked Sep 04 '26 06:09

SuperDuck


2 Answers

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
like image 115
jim31415 Avatar answered Sep 07 '26 17:09

jim31415


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.

like image 43
SuperDuck Avatar answered Sep 07 '26 17:09

SuperDuck