I have a chunk of text string @originalCommand within which I want to replace values $tablelist and @dms:
Currently I managed to get below working but some lines are repetitive actions.
SET @originalCommand = '$tablelist='table_1', $dms='dms_1''
PRINT '*** Original command text:';
PRINT @originalCommand;
--replace variables' values in ps script
SET @replaceStart = PATINDEX('%$tablelist=''%', @originalCommand) + LEN('$tablelist=''');
SET @replaceLength = PATINDEX('%''%', SUBSTRING(@originalCommand, @replaceStart, LEN(@originalCommand)));
IF @tableList = ''
SET @replaceString = CONCAT(@schemaName, '.', @tableName)
ELSE
SET @replaceString = @tableList;
SET @newCommand = REPLACE(@originalCommand, SUBSTRING(@originalCommand, @replaceStart, @replaceLength - 1), @replaceString);
SET @replaceStart = PATINDEX('%$dms=''%', @newCommand) + LEN('$dms=''');
SET @replaceLength = PATINDEX('%''%', SUBSTRING(@newCommand, @replaceStart, LEN(@newCommand)));
SET @replaceString = @dms;
SET @newCommand = REPLACE(@newCommand, SUBSTRING(@newCommand, @replaceStart, @replaceLength - 1), @replaceString);
PRINT '';
PRINT '*** New command text:';
PRINT @newCommand;
Expected result is working but I'm trying to achieve it without repeating the lines of replace code:
SET @dms = 'dms_new';
SET @tableName = 'table_new';
'$tablelist='table_new', $dms='dms_new''