Search code examples
sql-serverservice-broker

Service Broker: How can I detect the script is executed on the wanted machine?


My SQL scripts are generated from templates. They contain the IP address. There can be a lot of them and the operator can by mistake execute the incorrect script. How the script can check if it was launched on the correct machine? (Otherwise, I would like to print the message and exit.)

Is it possible at all?

Thanks, Petr


Solution

  • Updated Answer

    Here's a script that I found from http://weblogs.sqlteam.com/peterl/archive/2008/07/16/How-to-get-IP-address.aspx, with some modifications to better suit your needs:

    DECLARE @TargetIpAddress varchar(15);
    SET @TargetIpAddress = '127.0.0.1'; --<== The IP address of the server you want.
    
    DECLARE @Interfaces TABLE
    (
        RowID int IDENTITY(0, 1)
       ,Interface char(38)
       ,IP varchar(15)
    );
    
    INSERT @Interfaces ( Interface )
      EXEC master..xp_regenumkeys N'HKEY_LOCAL_MACHINE',
           N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces';
    
    DECLARE @RowID int
           ,@IP varchar(15)
           ,@Key nvarchar(200);
    
    SELECT @RowID = MAX(RowID)
      FROM @Interfaces;
    
    WHILE @RowID >= 0
    BEGIN
        SELECT @Key = N'System\CurrentControlSet\Services\TcpIP\Parameters\Interfaces\' + Interface
         FROM @Interfaces
        WHERE RowID = @RowID;
    
        EXEC master..xp_regread N'HKEY_LOCAL_MACHINE', @Key, N'DhcpIPAddress', @IP OUTPUT;
    
        IF @IP <> '0.0.0.0'
        UPDATE @Interfaces
           SET IP = @IP
         WHERE RowID = @RowID;
    
        SET @RowID = @RowID - 1;
    END;
    
    IF NOT EXISTS (SELECT IP FROM @Interfaces WHERE IP = @TargetIpAddress)
    BEGIN
        DECLARE @ErrorMessage varchar(2000);
        SET @ErrorMessage  = 'This is not the correct server. This server does not have an IP address of %s.';
        SET @TargetIpAddress = ISNULL(@TargetIpAddress, 'NULL');
        RAISERROR(@ErrorMessage, 16, 1, @TargetIpAddress);
    END
    
    -- The rest of the script...
    

    Original Answer

    It seems like using the Server\Instance name instead of the IP address would be easier to work with, and less likely to break if the server(s) were assigned different IP addresses at some later date.

    -- You can get the instance name like this:
    SELECT @@SERVERNAME +'\'+ @@SERVICENAME AS 'Instance';
    
    -- Although, you might prefer this instead:
    SELECT CAST(SERVERPROPERTY('MachineName') AS nvarchar(128))
          +COALESCE('\'+CAST(SERVERPROPERTY('InstanceName') AS nvarchar(128)), '');
    
    -- NetBIOS name of the local computer on which the instance of SQL Server
    -- is currently running.
    -- If the instance of SQL Server is in a failover cluster and you want to obtain
    -- the name of the failover clustered instance, use the MachineName property. 
    SELECT SERVERPROPERTY('ComputerNamePhysicalNetBIOS');
    

    You can find detailed information about the SERVERPROPERTY function at MSDN: SERVERPROPERTY (Transact-SQL). Although, this function doesn't provide any way to obtain the IP address of the server/instance - there is no built-in function that provides this information.