marrowyung
asked on
email out error message
hi all,
if I want to email out the SQL error message, like if I run this:
then if the user account doesn't not existing, and it gives error:
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.
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"
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.
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.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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?
"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?
ASKER
Victor,
error message don't show out what exactly it is .
i am testing this:
your vesrion will have error in the ()
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()
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
your vesrion will have error in the ()
ASKER
this works however:
but it doesn't tell which line tells this.. any idea?
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
but it doesn't tell which line tells this.. any idea?
ASKER
hi all,
I have a statement like this:
but as you all can see, for each IF begin statement, we have 3 x statemetn:
any way to make the error of each of them combine into one error message and send in one email ?
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
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';
any way to make the error of each of them combine into one error message and send in one email ?
ASKER
also when I am doing this:
I parse it and the error is:
any idea?
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
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".
any idea?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.
SET @err_msg = 'Error on line ' + CAST(ERROR_LINE() AS varchar) + ' - ' + ERROR_MESSAGE()
"
looks very execellent then... !
let me try.
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.
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()
ASKER
let me try tomorrow. tks
ASKER
Victor:
this one works well. tks.
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()
this one works well. tks.
ASKER
"BEGIN TRY should be after the USE Master and remove the GO and the BEGIN and END block:"
tks.
tks.
Good. Do you need something else for this question?
ASKER
just gave your mark.. you great
man.. relax.
man.. relax.
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