Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2005
    Posts
    319

    Unanswered: anyone good with batch files to add a SQL Alias programmatically? (I'm 95% there)

    Normally when we spin up a new instance we need to add an alias on one of our boxes so we can easily connect to it through SSMS using SQL Server Configuration Manager.

    I have written a batch file which adds the appropriate entry into the registry (it is actually two batch files, one for 32 bit and one for 64 bit). I am not sure how to get this to run with parameters though. I know %1 and %2 would be for the first two parameters, but when I run this, in the registry it actually puts %1 and %2 as the value pair.

    if you hardcode hostname and IP Address in place of %1 and %2 the batch file works as expected:
    Code:
    REGEDIT4
    
    ; @ECHO OFF
    ; CLS
    ; REGEDIT.EXE /S "%~f0"
    ; EXIT
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
    "%1"="DBMSSOCN,%2,1433"

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    You can "generate" a batch file based on paramters passed. This way you can do this:

    echo "%1"="DBMSSOCN,%2,1433" >>mynext.bat
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  3. #3
    Join Date
    Jun 2005
    Posts
    319
    that kinda helps, I found a dos batch forum o.O and made a post, I'll follow up with my final file here since some people may find it useful.

  4. #4
    Join Date
    Jun 2005
    Posts
    319
    I will probably roll with something like this...

    Code:
    USE [master]
    GO
    
    declare @p1 nvarchar(100)
    declare @p2 nvarchar(100)
    
    set @p1 = N'HostName'
    set @p2 = N'DBMSSOCN,1.2.3.4,1433'
    
    EXEC xp_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\Client\ConnectTo', @p1, REG_SZ, @p2
    GO

  5. #5
    Join Date
    Jun 2005
    Posts
    319
    and yet another solution I need to test:
    Code:
    @echo off
    reg add "HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo" /v "%~1" /t "REG_SZ" /d "DBMSSOCN,%~2,1433" /f

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •