Thursday, 22 September 2011

SQL @@SERVERNAME can return NULL, but it should not

Probably we are all using "@@SERVERNAME" variable for geting SQL Server name and usually it does what we expect.

Today some custom maintenance procedures stoped working. After investigation I've noticed that SELECT @@SERVERNAME returns "NULL".

Background: @@SERVERNAME gets value (at SQL Server boot) from table sys.servers where server_id is equal to 0.

SELECT [name] FROM sys.servers WHERE server_id = 0

in this system table there are also linked servers and "my server" was there with server_id different than zero.

EXEC master.dbo.sp_dropserver @server=N'serverName', @droplogins='droplogins'
EXEC sp_addserver 'serverName', LOCAL

and restart server.

Good workaround (probably best practice) is to use function ServerProperty('serverName').

More info at:

PS Server is SQL Server 2005 Standard

1 comment: