# Pass msq payload to SQL query where clause

**URL:** <https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231>\
**Category:** General\
**Created:** [23 March 2022 10:26 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231 "2022-03-23T10:26:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![QGIO](https://avatars.discourse-cdn.com/v4/letter/q/f17d59/32.png) [@QGIO](https://discourse.nodered.org/u/QGIO)\
**Post date:** [23 March 2022 10:26 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/1 "2022-03-23T10:26:36Z")

</div>

Hi node-red forum,

Is it possible to pass a msg payload to SQL query node to use in the where cluase?  
I mean somenthing like this:

 ![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a74d7539490cc691e870b7fa15a264df0d986347.png)

Which is the correct syntax?

The path of the msg payload to pass is payload.topic1[0].ID

---

<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:** [23 March 2022 10:35 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/2 "2022-03-23T10:35:10Z")

</div>

> [@QGIO](#):
>
> Which is the correct syntax

The "better" way is to use "Parameters"

> [@Node-red-contrib-mssql-plus =\> value in database is empty](https://discourse.nodered.org/t/node-red-contrib-mssql-plus-value-in-database-is-empty/52205/4):
>
> Hi, so a couple of things first. Please post code between backticks ``` like this ``` When copying values from the debug sidebar, use the "Copy Value" button that appears under your mouse cursor when you hover over the message [BX00Cy7yHi] On you your issue... The debug BEFORE SQL shows payload is an array but in your SQL, you try to access payload.ts\_pos\_wo\_id - an array is accessed by [] square brackets. To simplify things (and avoid mistakes) use the "Copy Path" button that ap…

---

<div class="post-metadata">

**Author:** ![QGIO](https://avatars.discourse-cdn.com/v4/letter/q/f17d59/32.png) [@QGIO](https://discourse.nodered.org/u/QGIO)\
**Post date:** [23 March 2022 10:50 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/3 "2022-03-23T10:50:06Z")

</div>

Thanks Steve.  
Also if I'm using a Select query (not Insert into)?

I want to make a merge between two table and extract only raw where the ID is the one passed by a previous function output.

---

<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:** [23 March 2022 10:52 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/4 "2022-03-23T10:52:33Z")

</div>

Parameters work with all types of queries (SELECT/UPDATE/INSERT/DELETE!

I dont think i fully understand what you are asking

Can you explain with a bit more detail please?

---

<div class="post-metadata">

**Author:** ![QGIO](https://avatars.discourse-cdn.com/v4/letter/q/f17d59/32.png) [@QGIO](https://discourse.nodered.org/u/QGIO)\
**Post date:** [23 March 2022 10:54 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/5 "2022-03-23T10:54:09Z")

</div>

Do you mean to use 'parameters' like in this way?

 ![immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/9092021e382d76b2afdd1c83636d770b4c8aad96.png)

---

<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:** [23 March 2022 10:58 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/6 "2022-03-23T10:58:42Z")

</div>

Sure, however you need to.

e.g.

↓ When the value `XXX` comes from `msg.payload[0].ID` ↓

then...

- `SELECT * FROM T1 where id=XXX` becomes
- `SELECT * FROM T1 where id=@ID`
- and add a parameter for `msg.payload[0].ID` named `ID`

---

<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:** [6 April 2022 10:59 UTC](https://discourse.nodered.org/t/pass-msq-payload-to-sql-query-where-clause/60231/7 "2022-04-06T10:59:19Z")

</div>

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