-- SQL code starts Sp_configure 'allow updates', 1 RECONFIGURE WITH OVERRIDE Go Use master Go If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [sp_password] ') and OBJECTPROPERTY (id, n' IsProcedure') = 1) Drop procedure [dbo]. [sp_password] Go Create procedure sp_password @ Old sysname = NULL, -- the old (current) password @ New sysname, -- the new password @ Loginame sysname = NULL -- user to change password on As -- Setup runtime options/declare variables -- Set nocount on Declare @ self int Select @ self = case when @ loginame is null THEN 1 ELSE 2 END -- RESOLVE LOGIN NAME If @ loginame is null Select @ loginame = suser_sname () -- Check permissions (SecurityAdmin per Richard Waymire )-- IF (not is_srvrolemember ('securityadmin') = 1) AND not @ self = 1 Begin Dbcc auditevent (107, @ self, 0, @ loginame, NULL) Raiserror (15210,-1,-1) Return (1) End ELSE Begin Dbcc auditevent (107, @ self, 1, @ loginame, NULL) End -- Disallow user transaction -- Set implicit_transactions off IF (@ trancount> 0) Begin Raiserror (15002,-1,-1, 'sp _ password ') Return (1) End -- Resolve login name (disallows nt names) If not exists (select * from master. dbo. syslogins where Loginname = @ loginame and isntname = 0) Begin Raiserror (15007,-1,-1, @ loginame) Return (1) End -- IF non-sysadmin attempting change to sysadmin, require password (218078 )-- If (@ self <> 1 AND is_srvrolemember ('sysadmin') = 0 AND exists (SELECT * FROM master. dbo. syslogins WHERE loginname = @ loginame and isntname = 0 AND sysadmin = 1 )) SELECT @ self = 1 -- Check old password if needed -- If (@ self = 1 or @ old is not null) If not exists (select * from master. dbo. sysxlogins Where srvid is null and Name = @ loginame and (@ Old is null and password is null) or (Pwdcompare (@ old, password, (case when xstatus & 2048 = 2048 THEN 1 ELSE 0 END) = 1 ))) Begin Raiserror (15211,-1,-1) Return (1) End -- Change the password -- Update master. dbo. sysxlogins Set password = convert (varbinary (256), pwdencrypt (@ new), xdate2 = getdate (), xstatus = xstatus &(~ 2048) Where name = @ loginame and srvid IS NULL -- Update protection timestamp for master db, to indicate syslogins change -- Exec ('use master grant all to Null ') -- FINALIZATION: return success/FAILURE -- If @ error <> 0 Return (1) Raiserror (15478,-1,-1) Return (0) -- sp_password GO Sp_configure 'allow updates', 0 RECONFIGURE WITH OVERRIDE -- SQL code ended |