Friday, June 06, 2014

Why is @@ServerName NOT the Name Of My Server?

I couldn't figure out why it was failing? It worked perfectly in the prototype environment that this was based on. In fact, these were clones of the machines.

Finally, I tracked it to the line

 select @replica_id = replicas.replica_id   
 from master.sys.availability_replicas as replicas   
 where   
    upper(replicas.replica_server_name COLLATE Latin1_General_CI_AS) = upper(@@SERVERNAME COLLATE Latin1_General_CI_AS) and group_id = @group_id  

 It turns out, that @@ServerName was returning a value that I had never heard of. It certainly wasn't my Sql Server name! But... it WAS the name of the cloned machine before it had been sysprepped and handed to me! It seems that @@ServerName uses a stored version of the servername.

If you want the current running name, use:

 SERVERPROPERTY('ServerName')   

Much better.

No comments: