Quantcast
Channel: SQL Archives - SQL Authority with Pinal Dave
Viewing all articles
Browse latest Browse all 594

SQL SERVER – How to Change Authentication Mode Using T-SQL Query

$
0
0

One of my clients asked an interesting question. It took a while for me to figure out the answer so sharing with my blog readers. In this blog, we would learn how to change authentication mode of SQL Server using T-SQL query instead of SQL Server Management Studio.

THE EMAIL WITH SITUATION

Hi Pinal,

I am an independent vendor of a software which uses SQL Express. I install SQL Server Express on client’s environment as a part of my software deployment. I realized that during installation I missed changing authentication mode to mixed mode and it installs SQL with Windows Authentication mode.

The challenge now is that the software is already shipped with this bug and I need to fix it quickly. There are few downloads so far, so I need to contact my clients and supply a fix. I know how to install SQL with mixed mode authentication, so I have already fixed it for further installations. I cannot ask my client to install SSMS on the machines to change the mode and then uninstall it.

Do you have any solution for me? I am not well-versed with SQL so there might be an easy answer, but I just do not have time to search on the internet. You can imagine the pressure to get the fix released for already downloads.

Awaiting reply!

WORKAROUND/SOLUTION

As you can see in the above email, he wanted quick help. So, I made an offer, he accepted, and we started working. He told that he knows that below is what needs to be done and restart SQL.

SQL SERVER - How to Change Authentication Mode Using T-SQL Query mixed-auth-tsql-01

Here is the error we would see in ERROLROG when we try to use SQL account to log in and SQL is configured with “Windows Authentication mode”.

Error: 18456, Severity: 14, State: 58.
Login failed for user ‘AppLogin’. Reason: An attempt to login using SQL authentication failed. The server is configured for Windows authentication only. [CLIENT: <local machine>]

Here is the T-SQL to do the same.

  1. To set mode to “Windows Authentication” (LoginMode= 1)
USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', 
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 1
GO
  1. To set mode to “SQL Server and Windows Authentication mode” (LoginMode= 2)
USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', 
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
GO

I must tell you that my client was smart. He wrote a batch file to change the registry key for the instance which software installed and to restart the SQL Service for that instance. After this software also started working and we both were happy.

Reference: Pinal Dave (https://blog.sqlauthority.com)

First appeared on SQL SERVER – How to Change Authentication Mode Using T-SQL Query


Viewing all articles
Browse latest Browse all 594

Trending Articles