Avatar of marrowyung
marrowyung

asked on 

email out error message

hi all,

if I want to email out the SQL error message, like if I run this:

sp_change_users_login "update_one", "AdminProxy", "AdminProxy" 
sp_change_users_login "update_one", "CybernetUser", "CybernetUser" 

Open in new window


then if the user account doesn't not existing, and it gives error:

Msg 15291, Level 16, State 1, Procedure sp_change_users_login, Line 137
Terminating this procedure. The Login name 'AdminProxy' is absent or invalid.
Msg 15291, Level 16, State 1, Procedure sp_change_users_login, Line 137
Terminating this procedure. The Login name 'CybernetUser' is absent or invalid.

Open in new window


how can I have SQL server to email out the error message to me if I run all these in a SP, I don't want it to be an SQL job failure message as that statement only part of the whole logic. Not the whole logic of the total failure of a job.
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
marrowyung
SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jason clark
Jason clark
Flag of United States of America image

If you run EXEC sp_change_users_login 'report' you get the list of orphaned users.
FIX the error using query given below. EXEC sp_change_users_login 'auto_fix', 'UserName'
I would first try to check whether a LOGIN "NAME" exists on the server (authentication).

If the login does not exist create the login and rerun the sp_change_users_login with 'auto_fix'
Follow this link may it help yours http://sqlserverdb.blogspot.in/2011/04/login-invalid-for-orphand-user.html
Avatar of marrowyung
marrowyung

ASKER

Jason clark,

"If you run EXEC sp_change_users_login 'report' you get the list of orphaned users.
FIX the error using query given below. EXEC sp_change_users_login 'auto_fix', 'UserName'
I would first try to check whether a LOGIN "NAME" exists on the server (authentication)."

I knew that but my point is, I will see some doesn't exist user and I want to email out the error message one by one.

Vitor,

so the "ERROR_MESSAGE(),"

is the SQL output (if it is an error) and email it out one email of each user when doing sp_change_users_login  ? and if only some of the user has problem the process is not going to stop and any of the these error can be send out?
Avatar of marrowyung
marrowyung

ASKER

Victor,

error message don't show out what exactly it is .

 i  am testing this:

DECLARE @err_msg AS NVARCHAR(MAX);

SET @err_msg = ERROR_MESSAGE()

Open in new window


 begin catch
	 EXEC msdb.dbo.sp_send_dbmail 
		  @profile_name = 'No Reply', 
		  @recipients = 'marrow.yung@cds.com.hk', -- 
		  @body = @err_msg ,
		  -- @body = @UserFail,
		  @subject = 'Error when running sp_change_users_login '
  end catch

Open in new window


your vesrion will have error in the ()
Avatar of marrowyung
marrowyung

ASKER

this works however:

 
DECLARE @err_msg AS NVARCHAR(MAX);

.
.
.
begin catch

 SET @err_msg =  ERROR_MESSAGE();
	 EXEC msdb.dbo.sp_send_dbmail 
		  @profile_name = 'No Reply', 
		  @recipients = 'marrow.yung@cds.com.hk', -- 
		  @body = @err_msg ,
		  @subject = 'Error when running sp_change_users_login '
  end catch

Open in new window


but it doesn't tell which line tells this.. any idea?
Avatar of marrowyung
marrowyung

ASKER

hi all,

I have a statement like this:

IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'XXXXXX\aaaConsult' and TYPE = 'U')
    BEGIN
        CREATE USER [XXXXXX\aaaConsult] FOR LOGIN [XXXXXX\aaaConsult] WITH DEFAULT_SCHEMA=[dbo];
        
        EXECUTE sp_addrolemember 'Extol Developer', 'XXXXXX\aaaConsult';
        EXECUTE sp_addrolemember 'db_datareader', 'XXXXXX\aaaConsult';
    END;        
GO

GO
IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = 'XXXXXX\extolconsultant2' and TYPE = 'U')
    BEGIN
        CREATE USER [XXXXXX\extolconsultant2] FOR LOGIN [XXXXXX\extolconsultant2] WITH DEFAULT_SCHEMA=[dbo];

        EXECUTE sp_addrolemember 'Extol Developer', 'XXXXXX\extolconsultant2';
        EXECUTE sp_addrolemember 'db_datareader', 'XXXXXX\extolconsultant2';

    END;
GO

Open in new window


but as you all can see, for each IF begin statement, we have 3 x statemetn:

    CREATE USER [XXXXXX\extolconsultant2] FOR LOGIN [XXXXXX\extolconsultant2] WITH DEFAULT_SCHEMA=[dbo];

        EXECUTE sp_addrolemember 'Extol Developer', 'XXXXXX\extolconsultant2';
        EXECUTE sp_addrolemember 'db_datareader', 'XXXXXX\extolconsultant2';

Open in new window


any way to make the error of each of them combine into one error message and send in one email ?
Avatar of marrowyung
marrowyung

ASKER

also when I am doing this:

DECLARE @err_msg AS NVARCHAR(MAX);
BEGIN
begin try

use master;
go
 
exec sp_removedbreplication

 end try
 begin catch

 SET @err_msg =  ERROR_MESSAGE();
	 EXEC msdb.dbo.sp_send_dbmail 
		  @profile_name = 'No Reply', 
		  @recipients = 'dddd@bbb.com.hk', -- 
		  @body = @err_msg ,
		  @subject = sdfdsfs '
  end catch
  
  
  
  END
  

Open in new window


I parse it and the error is:

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near 'try'.
Msg 137, Level 15, State 1, Line 7
Must declare the scalar variable "@err_msg".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@err_msg".

Open in new window


any idea?
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of marrowyung
marrowyung

ASKER

"ERROR_LINE is what you want then:
SET @err_msg =  'Error on line ' + CAST(ERROR_LINE() AS varchar) + ' - ' + ERROR_MESSAGE()
"

looks very execellent then... !

let me try.
Avatar of marrowyung
marrowyung

ASKER

but the normal error message is :

Msg 15291, Level 16, State 1, Procedure sp_change_users_login, Line 138

however, the one using yours:

Error on line 138 only.
You can get all those information from ERROR_* functions:
SET @err_msg = 'Msg ' + CAST(ERROR_NUMBER() AS varchar) + ', Level ' + CAST(ERROR_SEVERITY() AS varchar) + ', State ' + CAST(ERROR_STATE() AS varchar) + ', Procedure ' + ERROR_PROCEDURE() + + ', Line ' + CAST(ERROR_LINE() AS varchar) + CHAR(13) + CHAR(10) + ERROR_MESSAGE() 

Open in new window

Avatar of marrowyung
marrowyung

ASKER

let me try tomorrow. tks
Avatar of marrowyung
marrowyung

ASKER

Victor:

SET @err_msg = 'Msg ' + CAST(ERROR_NUMBER() AS varchar) + ', Level ' + CAST(ERROR_SEVERITY() AS varchar) + ', State ' + CAST(ERROR_STATE() AS varchar) + ', Procedure ' + ERROR_PROCEDURE() + + ', Line ' + CAST(ERROR_LINE() AS varchar) + CHAR(13) + CHAR(10) + ERROR_MESSAGE() 

Open in new window


this one works well. tks.
Avatar of marrowyung
marrowyung

ASKER

"BEGIN TRY should be after the USE Master and remove the GO and the BEGIN and END block:"


tks.
Good. Do you need something else for this question?
Avatar of marrowyung
marrowyung

ASKER

just gave your mark.. you great

man.. relax.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo