Here is a solution that uses the ability to call Python script in SQL Server. To be able to use this solutions, there are a few preconditions that needs to be fulfilled.
- The Python support must be installed. This is an optional install. It can be added later.
- The server configuration parameter external scripts enabled must be 1.
- The user running the SQL code must have the database permission EXECUTE ANY EXTERNAL SCRIPT.
Since I was mainly interested in the general problem, to replace parameter placeholders in strings with data taken from a table, I simplified how the parameters are in the string. In the example below, a parameter appears simply as something enclosed in braces, for instance {param}
. I leave it as an exercise to you to implement support for your more complex parameter holders. If you have worked with regular expressions in Python before, it should be that tricky.
DROP TABLE IF EXISTS DataTable
CREATE TABLE DataTable (
id int,
notes varchar(1000)
);
INSERT INTO DataTable VALUES
(1, 'develop document disseminate to {ac-1_prm_1} and {one_more_par} for the benefit of Mr Kite'),
(2, 'develop document to {ac-1_prm_2}'),
(3, 'The Organization {no_such_param} is winning the stakes');
DECLARE @DataTableParameter TABLE (
parameterid varchar(100),
[value] varchar(100),
id int
);
INSERT INTO @DataTableParameter VALUES
('ac-1_prm_1', 'apple doc.', 1),
('ac-1_prm_2', 'google doc.', 1),
('ac-1_prm_3', 'facebook doc.', 2),
('one_more_par', 'other sources', 3)
DECLARE @createdict nvarchar(MAX)
SELECT @createdict = 'params = {' +
string_agg(quotename(parameterid, '''') + ':' + quotename(value, ''''), ', ') + '}'
FROM @DataTableParameter
SELECT @createdict
DECLARE @pyscript nvarchar(MAX) = N'
import re, pandas
def paramrepl(matchobj):
param = matchobj.group(1)
if param in params:
return params[matchobj.group(1)]
else:
return "{" + param + "}"
' + @createdict + '
Data["notes"] = pandas.Series(
[re.sub(r"\{([^\}]+)\}", paramrepl, str) for str in Data["notes"]])
'
EXEC sp_execute_external_script
@language = N'Python',
@input_data_1 = N'SELECT id, notes FROM DataTable',
@input_data_1_name = N'Data',
@output_data_1_name = N'Data',
@script = @pyscript
WITH RESULT SETS ((id int,
notes varchar(1000)))