Unsolved
This post is more than 5 years old
1 Rookie
•
8 Posts
0
1266
June 18th, 2017 09:00
DDBMA 3.5 help with T-SQL script
Hi,
I'm very beginner with TSQL and sql query scripting and have been trying to modify the default generated TSQL script provided by the DDBMA plugin to do what I want, which is to pass it variables instead of one static line of parameters while still getting a proper return error code if it fails. The way I've been testing the return code is to terminate the backup process in the middle of a backup. Here's the default script generated with "static" parameters by the DDBMA GUI in which the returncode check works as expected:
USE [master]
GO
DECLARE @returnCode int
EXEC @returnCode = dbo.emc_run_backup ' -c someserver.somedomain.com -l full -y +30d -a "NSR_DFA_SI=TRUE" -a "NSR_DFA_SI_USE_DD=TRUE" -a "NSR_DFA_SI_DD_HOST=datadomainhost.somedomain.com" -a "NSR_DFA_SI_DD_USER=ddboost" -a "NSR_DFA_SI_DEVICE_PATH=/dd01_sql1" -a "NSR_DFA_SI_DD_LOCKBOX_PATH=\\servername\lockbox" -a "NSR_SKIP_NON_BACKUPABLE_STATE_DB=TRUE" -a "SKIP_BACKUP_PROMOTION=FALSE" "MSSQL:"'
IF @returnCode <> 0
BEGIN
RAISERROR ('Fail!', 16, 1)
END
...So I figured out as much as replacing the static passed values with variables instead and needing to use some form of dynamic sql to exec it all, but I haven't been able to figure out how to get back to getting a proper return code. I was able to make this type of execution work:
set @commandString = 'dbo.emc_run_backup '' -c ' + @clientname + ' -S ' + @backupStripes + ' -l ' + @backupLevel + ' -y +' + @retention + 'd -a "NSR_DFA_SI=TRUE" -a "NSR_DFA_SI_USE_DD=TRUE" -a "NSR_DFA_SI_DD_HOST=' + @dataDomain + '" -a "NSR_DFA_SI_DD_USER=' + @ddboostUser + '" -a "NSR_DFA_SI_DEVICE_PATH=/' + @ddboostStorageUnit + '" -a "NSR_DFA_SI_DD_LOCKBOX_PATH=' + @lockboxPath + '" -a "NSR_SKIP_NON_BACKUPABLE_STATE_DB=TRUE" -a "SKIP_BACKUP_PROMOTION=FALSE" -N "' + @labelStamp + '" "' + @dbName + '"'''
exec(@commandString)
...the above executes OK, but couldn't get @returnCode working anywhere in this way, and trying to return @@error is always 0. Reading around it sounds like I need to use 'exec sp_executesql' instead and return it by output. I switched @commandString to be nvarchar. The below was as close as I could get but I'm having syntax issues that seems like it is mostly due to quote handling. I think I had it properly execute the statement once but lost the output status text of the backup command this way. I'd still like to see the standard output of the command to my results window like the first two examples do, while getting a proper returnCode for the if/raiserror check in the first example. Any help here is greatly appreciated!
set @commandString = '''dbo.emc_run_backup '''' -c ' + @clientname + ' -S ' + @backupStripes + ' -l ' + @backupLevel + ' -y +' + @retention + 'd -a "NSR_DFA_SI=TRUE" -a "NSR_DFA_SI_USE_DD=TRUE" -a "NSR_DFA_SI_DD_HOST=' + @dataDomain + '" -a "NSR_DFA_SI_DD_USER=' + @ddboostUser + '" -a "NSR_DFA_SI_DEVICE_PATH=/' + @ddboostStorageUnit + '" -a "NSR_DFA_SI_DD_LOCKBOX_PATH=' + @lockboxPath + '" -a "NSR_SKIP_NON_BACKUPABLE_STATE_DB=TRUE" -a "SKIP_BACKUP_PROMOTION=FALSE" -N "' + @labelStamp + '" "' + @dbName + '"'''
set @sql = N'set @err = '+@commandString
Exec sp_executesql @sql, N'@err int output', @err = @returnCode output
events found

