Skip to main content

Posts

Showing posts from 2021

Fix SQL database User accounts when doing a backup and restore.

-- select the desired database to fix the user accounts --Use ; EXEC sp_change_users_login 'report'--See all orphaned users in the database. DECLARE @OrphanedUsers TABLE ( IndexKey Int IDENTITY(1,1) PRIMARY KEY, UserName SysName,--nVarChar(128) UserSID VarBinary(85) ) INSERT INTO @OrphanedUsers EXEC sp_change_users_login 'report' DECLARE @CRLF as nVarChar SET @CRLF = CHAR(10) + '&' + CHAR(13)--NOTE: Carriage-Return/Line-Feed will only appear in PRINT statements, not SELECT statements. DECLARE @Sql as nVarChar(MAX) SET @Sql = N'' DECLARE @IndexKey as Int SET @IndexKey = 1 DECLARE @MaxIndexKey as Int SET @MaxIndexKey = (SELECT COUNT(*) FROM @OrphanedUsers) DECLARE @Count as Int SET @Count = 0 DECLARE @UsersFixed as nVarChar(MAX) SET @UsersFixed = N'' DECLARE @UserName as SysName--This is an orphaned Database user. WHILE (@IndexKey <= @MaxIndexKey) BEGIN SET @UserName = (SELECT UserName FROM @Or

Script out EXISTING database mail profile's settings

  Script which will script out your present SQL Server 2016 Database Mail profiler Setup and Configuration Scripts. It will give you the script to Drop and create Settings For DatabaseMail profiles. Note: Tested on SQL 2005, to SQL Server 2016 Versions. USE msdb GO Declare @ TheResults varchar ( max ), @ vbCrLf CHAR ( 2 ) SET @ vbCrLf = CHAR ( 13 ) + CHAR ( 10 ) SET @ TheResults = ' use master go sp_configure ''show advanced options'',1 go reconfigure with override go sp_configure ''Database Mail XPs'',1 --go --sp_configure ''SQL Mail XPs'',0 go reconfigure go ' SELECT @ TheResults = @ TheResults + ' -------------------------------------------------------------------------------------------------- -- BEGIN Mail Settings ' + p . name + ' -------------------------------------------------------------------------------------------------- IF NOT EXISTS(SELECT * FRO