# MSSQL-Plus - Appending the Query output to incoming payload.msg

**URL:** <https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926>\
**Category:** General\
**Created:** [19 August 2021 15:25 UTC](https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926 "2021-08-19T15:25:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tusharacc](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tusharacc](https://discourse.nodered.org/u/tusharacc)\
**Post date:** [19 August 2021 15:25 UTC](https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926/1 "2021-08-19T15:25:30Z")

</div>

I am building a simple workflow using Node-Red and Ms-SQL. The MS-SQL Node receives an dictionary object with multiple key-value. MS-SQL node is executing in query mode, and returns value from database. My requirement is - to append the value that is received from query to the incoming Payload.msg. I have modified my query to attain this -

```auto
Select '{{{payload.ProductType}}}' As PolicyType,
'{{{payload.RuleType}}}' As RuleType,
'{{{payload.SystemAssignId}}}' As SystemAssignId,
'{{{payload.TransTypeCd}}}' As TransType,
'{{{payload.TransEffDt}}}' As TransEffDt,
'{{{payload.AddlRetPremAmt}}}' As AddlPrem,
'{{{payload.TermPremAmt}}}' As TermPrem,
'{{{payload.PolicyExpDt}}}' As PolicyExp,
'{{{payload.PolicyEffDt}}}' As PolicyEff,
a.PolicyLossInd,b.LossDt, b.LossPaidAmt From [MyTableA] (nolock) a
left outer join MyTableB] b on b.SystemAssignId = a.SystemAssignId and b.TransSeqNo = a.TransSeqNo
Where a.SystemAssignId = '{{{payload.SystemAssignId}}}' and a.TransSeqNo = 1

```

This works well, however there is a slight problem. If the incoming message payload key:value changes, I need to update the query as well, which is bad. Hence I believe there might be a better way of handling this issue. Any suggestions/pointer

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [20 August 2021 06:46 UTC](https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926/2 "2021-08-20T06:46:29Z")

</div>

So are you appending values from payload into the SQL query so that every row returned has these values?

If you simply want to merge the payload with the results rows you can do that after the SQL has executed.

---

<div class="post-metadata">

**Author:** ![tusharacc](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tusharacc](https://discourse.nodered.org/u/tusharacc)\
**Post date:** [24 August 2021 19:29 UTC](https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926/3 "2021-08-24T19:29:06Z")

</div>

@Steve-Mcl , Not sure if I understand your solution. I have found another solution. I have realized that msg object (if not destroyed) persists throughout the flow. So before calling database, I have a function call that will inject all the keys in msg.payload to msg.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 October 2021 19:29 UTC](https://discourse.nodered.org/t/mssql-plus-appending-the-query-output-to-incoming-payload-msg/49926/4 "2021-10-23T19:29:34Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
