# SELECT from SQL into variable

**URL:** https://discourse.nodered.org/t/select-from-sql-into-variable/19605
**Category:** General
**Created:** [26 December 2019 20:54 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605 "2019-12-26T20:54:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kajoty](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kajoty](https://discourse.nodered.org/u/kajoty)
#### Post date: [26 December 2019 20:54 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/1 "2019-12-26T20:54:56Z")

</div>

Hello.  
I need again some help with SQL and NR. I've stored some temp datas into a SQL db. Now I want to "ask" the db for the latest temp.  
The db is like this  
column "1" = AUTO\_INCREMENT   
column "temperature\_C" = the temp. My function loooks like:

```auto
var query;
query ="SELECT Wettersensoren.temperature_C FROM Wettersensoren ORDER BY Wettersensoren.1 DESC LIMIT 1";

flow.set("flowtmp",query);
msg.payload = query;
msg.topic = "Temperatur";
return msg;

```

The debug output is

```auto
"Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Temperatur' at line 1"

```

Could anybody give me a hint how to do it correctly?!?

Many thanks and merry christmas 🙂

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 December 2019 21:06 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/2 "2019-12-26T21:06:52Z")

</div>

Which sql node are you using (node-red-contrib-something possibly). Some of them expect the query in the topic not the payload.

---

<div class="post-metadata">

### Author: ![kajoty](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kajoty](https://discourse.nodered.org/u/kajoty)
#### Post date: [27 December 2019 09:22 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/3 "2019-12-27T09:22:16Z")

</div>

I'm using node-red-node-mysql

my flow looks like this:

inject -\> function -\> node-red-node-mysql -\> debug

I'm pretty sure the SQL works. Because I'm feeding the database with tempreture datas.

My "aim" is to store the latest temp. in the flow.set "FLOWTMP" and send this together with some text by "telegram" to my cell-phone.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [27 December 2019 10:07 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/4 "2019-12-27T10:07:20Z")

</div>

As Colin mentioned earlier in this thread, you need the query to be in msg.topic.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [27 December 2019 10:07 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/5 "2019-12-27T10:07:35Z")

</div>

Where does the info tab say the query should be placed and what have you got in there? Feed the output of your function node into a debug node set to Show Complete Message and see whether what you see matches what the node expects.

---

<div class="post-metadata">

### Author: ![kajoty](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kajoty](https://discourse.nodered.org/u/kajoty)
#### Post date: [27 December 2019 11:54 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/6 "2019-12-27T11:54:43Z")

</div>

```auto
Temperatur : msg.payload : string[94]

"SELECT Wettersensoren.temperature_C FROM Wettersensoren ORDER BY Wettersensoren.1 DESC LIMIT 1"

```

this is the debug output from the function

It seems to that the SQL SELECT is not processed.. it just output the text

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [27 December 2019 12:02 UTC](https://discourse.nodered.org/t/select-from-sql-into-variable/19605/7 "2019-12-27T12:02:44Z")

</div>

This shows you have putt the query into msg.payload.

As the sidebar help for the node explains, and others have pointed out here, it needs to be msg.topic.
