Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:26:34+00:00 2026-06-13T18:26:34+00:00

My SQL scripts are generated from templates. They contain the IP address. There can

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:26:36+00:00Added an answer on June 13, 2026 at 6:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have several SQL scripts which are generated from an Entity Model. They need
I would like to store lengthy .sql scripts in my solution and execute them
Is it possible to generate scripts from SQL Server 2008 that can then be
This is from a sql script. What tool can generate this? Thanks --USE [MY_TABLE]
In Microsoft SQL Server Management Studio 2008 there is a Generate scripts... option under
I generated a SQL script from a C# application on Windows 7. The name
I need to export database from SQL Server 2005 to SQL scripts (like we
I can't seem to execute SQL that creates a database using a DbCommand object.
I want to generate a whole lot of SQL*Plus scripts by querying the data
Traditionally I have always written my sql scripts by hand so they are nice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.