title: Configure SQL Server startup parameters from batch file
body: <p>I need to add start-up trace flags to a SQL Service from a batch file. I have struggled to work out how to do this, and have finally come-up with the following, slightly clumsy approach, of using undocumented TSQL procedure and then calling it with SQLCMD. The problem is that it throws an error: RegCreateKeyEx() returned error 5, 'Access is denied.' However, another script on the same server, allows me to change other registry values, such as TCP Port. Any idea what I'm doing wrong, or a more elegant way of doing it?</p>
  
  <pre><code>    DECLARE @VALUE nvarchar(200)
  DECLARE @Key nvarchar(2000)
  
  SET @VALUE = '-T1118 -3604 -E'
  
  SET @Key = 'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.' + (select SUBSTRING(@@SERVERNAME,CHARINDEX('\',@@SERVERNAME)+1,LEN(@@SERVERNAME)-1)) + '\MSSQLServer\Parameters'
  
  EXECUTE master..xp_regwrite
   'HKEY_LOCAL_MACHINE',
   @Key,
  'SQLArg3',
  'REG_SZ',
  @VALUE
  </code></pre>
  
  <p>The expression to create the @Key variable elavuates to: SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MYINSTANCE\MSSQLServer\Parameters
  which seems correct to me.</p>