Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 98
  • Last Modified:

How to use RaiseError in my stored proc

I am running a backup and restore script in a stored proc, which is run on a sql agent job. If the backup or the restore fails, the sql agent step still shows completed successfully. How can I use RaiseError in my stored proc script so the sql agent step does show failed if backup or restore failed.

Here is my Sproc script:

USE [My_data]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[bckup_restr_my_db]
as

--archive   
declare @exitcode integer                      
declare @sqlerrorcode integer                                                              
declare @dt varchar(25)                      
declare @exeString varchar(500)    
declare @exeString2 varchar(500)      
        
                    
                      
select @dt = replace(replace(convert(varchar(25), getdate()), ' ', '_'), ':', '')                      
set @exeString = N'-SQL "BACKUP DATABASE [My_data] TO DISK = 
''E:\MSSQL\My_data_' + @dt + '.sqb'' WITH ERASEFILES = 2, DISKRETRYINTERVAL = 30, DISKRETRYCOUNT = 10, COMPRESSION = 4, THREADCOUNT = 7"'  
                      
EXECUTE [MyETLServer].master..sqlbackup @exeString, @exitcode OUTPUT, @sqlerrorcode OUTPUT  

if @exitcode <> 0                       
return 1      


set @exeString2 =  N'-SQL "RESTORE DATABASE [My_data] FROM DISK = ''\\MyETLServer\MSSQL\My_data_' + @dt + '.sqb'' WITH RECOVERY, DISCONNECT_EXISTING,  MOVE ''My_data'' TO ''Y:\MSSQL-G1-Data\Data\My_data.mdf'', MOVE ''My_data_log'' TO ''Y:\MSSQL-G1-Logs\Logfiles\My_data_log.ldf'', REPLACE, ORPHAN_CHECK"'
EXECUTE [MyProdServer].master..sqlbackup @exeString2, @exitcode OUTPUT, @sqlerrorcode OUTPUT 

if @exitcode <> 0                       
return 1     

Open in new window

0
patd1
Asked:
patd1
3 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Before the Return 1 statement add a Raise Error command. Example:
RAISERROR ('Error during Backup & Restore procedure.', -- Error message text.
               16, -- Severity.
               1 -- State.
               )

Open in new window

0
 
ste5anSenior DeveloperCommented:
Cause you have no error handling and are always returning 1 as result code..
0
 
Racim BOUDJAKDJICommented:
Just to get you started...Note that sp_msforeachdb is not supported but is fine to use in BACKUP process.

exec sp_msforeachdb '
if lower(''?'') not in (''tempdb'')
begin
	backup database [?] to disk= ''C:\[?]_.BAK'';

	if @@error  <> 0
	begin
		raiserror (''Failure of [?] backup.'', 16, 1)
	end
end'

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
patd1Author Commented:
Thanks for your help Vitor Montalvão.

If exitcode <> 0 that means it was error, so I raise error. But why is it not failing as it returns 1?

Please review the code below, if that is what is needed.

if @exitcode <> 0          
            RAISERROR ('Error during Backup & Restore procedure.', -- Error message text.
               16, -- Severity.
               1 -- State.
               ) 
return 1  

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
You need a BEGIN END block since there's more than one line of code after the IF:
if @exitcode <> 0          
BEGIN
        RAISERROR ('Error during Backup & Restore procedure.', -- Error message text.
               16, -- Severity.
               1 -- State.
               ) 
     return 1
END  

Open in new window

0
 
patd1Author Commented:
Thank you.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now