Skip to content
Advertisement

‘Merge Fields’ – alike SQL Server function

I try to find a way to let the SGBD perform a population of merge fields within a long text.

Create the structure :

Now, create test values

At the time being my app is delivering the final statement, looping through merges, replacing in the stored text and output

Waw, stackoverflow is an amazing library of lost people in the IT hell, and i have the feeling that 85% of the users found a solution, personally I asked 12 questions.

I try to find a way to be code-independent and serve the output in a single query, as u understood, select a statement in which the stored text have been populated with user data. I hope I’m clear.

I looked on TRANSLATE function but it looks like a char replacement, so I have two choices :

  • I try a recursive function, replacing one by one until no merge_fields is found in the calculated text; but I have doubts about the performance of this approach;
  • There is a magic to do that but I need your knowledge…

Consider that I want this because the real texts are very long, and I don’t want to store it more than once in my database. You can imagine a 3 pages contract with only 12 parameters, like start date, invoiced amount, etc… Everything else cant be changed for compliance.

Thank you for your time!

EDIT :

Thanks to Randy’s help, this looks to do the trick :

I will check with a bigger database if the performance is good…

At least, I can “populate” one statement, I need to figure out to be able to extract a list as well.

Thanks again !

Advertisement

Answer

If a record is updated more than once by the same update, the last wins. None of the updates are affected by the others – no cumulative effect. It is possible to trick SQL using a local variable to get cumulative effects in some cases, but it’s tricky and not recommended. (Order becomes important and is not reliable in an update.)

One alternate is recursion in a CTE. Generate a new record from the prior as each token is replaced until there are no tokens. Here is a working example that replaces 1 with A, 2 with B, etc. (I wonder if there is some tricky xml that can do this as well.)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement