SQL Server Trying to programmatically add a SQL Alias to the registry

wljmcqd8  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(162)

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:

REGEDIT4

; @ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo]
"%1"="DBMSSOCN,%2,1433"
tmb3ates

tmb3ates1#

As an alternative, you could (with appropriate permissions) try to write to the registry with T-SQL:

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
eimct9ow

eimct9ow2#

To pass a parameter in a batch file, use %%1, %%2, etc

相关问题