SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 How to change authentication mode in SQL Server
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SwePeso
Patron Saint of Lost Yaks

Sweden
30218 Posts

Posted - 06/19/2008 :  11:04:43  Show Profile  Visit SwePeso's Homepage  Reply with Quote
CREATE PROCEDURE dbo.uspSetSQLServerAuthenticationMode
(
	@MixedMode BIT
)
AS

SET NOCOUNT ON

DECLARE @InstanceName NVARCHAR(1000),
	@Key NVARCHAR(4000),
	@NewLoginMode INT,
	@OldLoginMode INT

EXEC master..xp_regread	N'HKEY_LOCAL_MACHINE',
			N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL\',
			N'MSSQLSERVER',
			@InstanceName OUTPUT

IF @@ERROR <> 0 OR @InstanceName IS NULL
	BEGIN
		RAISERROR('Could not read SQL Server instance name.', 18, 1)
		RETURN -100
	END

SET	@Key = N'Software\Microsoft\Microsoft SQL Server\' + @InstanceName + N'\MSSQLServer\'

EXEC master..xp_regread	N'HKEY_LOCAL_MACHINE',
			@Key,
			N'LoginMode',
			@OldLoginMode OUTPUT

IF @@ERROR <> 0
	BEGIN
		RAISERROR('Could not read login mode for SQL Server instance %s.', 18, 1, @InstanceName)
		RETURN -110
	END

IF @MixedMode IS NULL
	BEGIN
		RAISERROR('No change to authentication mode was made. Login mode is %d.', 10, 1, @OldLoginMode)
		RETURN -120
	END

IF @MixedMode = 1
	SET	@NewLoginMode = 2
ELSE
	SET	@NewLoginMode = 1

EXEC master..xp_regwrite	N'HKEY_LOCAL_MACHINE',
				@Key,
				N'LoginMode',
				'REG_DWORD',
				@NewLoginMode

IF @@ERROR <> 0
	BEGIN
		RAISERROR('Could not write login mode %d for SQL Server instance %s. Login mode is %d', 18, 1, @NewLoginMode, @InstanceName, @OldLoginMode)
		RETURN -130
	END

RAISERROR('Login mode is now %d for SQL Server instance %s. Login mode was %d before.', 10, 1, @NewLoginMode, @InstanceName, @OldLoginMode)
RETURN 0



E 12°55'05.25"
N 56°04'39.16"
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000