# Logging data in to MSSQL

**URL:** https://discourse.nodered.org/t/logging-data-in-to-mssql/67539
**Category:** Industrial
**Created:** [10 September 2022 05:21 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539 "2022-09-10T05:21:02Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 05:21 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/1 "2022-09-10T05:21:02Z")

</div>

Hi ,  
I am facing issues in logging data (inserting) data in to mssql server.I am posting my flow..can someone please help me find issue..

```auto
[
    {
        "id": "196afc4293e110c7",
        "type": "function",
        "z": "6f4fb60b82629334",
        "name": "",
        "func": "temp = Number(global.get(\"rtdpco2\")).toFixed(2);\ndoorstatus = global.get(\"dipco2lwr\");\nupper_limit = global.get(\"pco2-up\");\nlower_limit = global.get(\"pco2-low\");\nmsg.topic = \"INSERT INTO pco2 (door_status,temp,upper_limit,lower_limit) VALUES(\" + doorstatus + \",\" + temp +\",\" + upper_limit +\",\" + lower_limit +\");\"\nreturn msg;",
        "outputs": 1,
        "noerr": 8,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 580,
        "y": 360,
        "wires": [
            [
                "ee57a1d4778d19aa"
            ]
        ]
    },
    {
        "id": "ee57a1d4778d19aa",
        "type": "MSSQL",
        "z": "6f4fb60b82629334",
        "mssqlCN": "358e71158db79bf8",
        "name": "read",
        "outField": "payload",
        "returnType": 0,
        "throwErrors": 1,
        "query": "",
        "modeOpt": "queryMode",
        "modeOptType": "query",
        "queryOpt": "payload",
        "queryOptType": "editor",
        "paramsOpt": "",
        "paramsOptType": "editor",
        "rows": "rows",
        "rowsType": "msg",
        "params": [],
        "x": 770,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "255a54677492919b",
        "type": "inject",
        "z": "6f4fb60b82629334",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 420,
        "y": 360,
        "wires": [
            [
                "196afc4293e110c7"
            ]
        ]
    },
    {
        "id": "358e71158db79bf8",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "",
        "server": "localhost",
        "port": "1433",
        "encyption": false,
        "trustServerCertificate": false,
        "database": "DATA",
        "useUTC": false,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5",
        "parseJSON": false,
        "enableArithAbort": true
    }
]

```

""TypeError: this.tail.search is not a function"---thats the error I am getting

---

<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: [10 September 2022 07:47 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/2 "2022-09-10T07:47:55Z")

</div>

In order to make code and flows readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Which mssql node are you using? I understand the the mssql-plus node is the one recommended currently.

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 07:54 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/3 "2022-09-10T07:54:13Z")

</div>

I am using mssql plus node with mssql 2019 connected to it.

Edited the previous post as requested.

---

<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: [10 September 2022 07:59 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/4 "2022-09-10T07:59:39Z")

</div>

> [@joey\_ind](#):
>
> Edited the previous post as requested.

Those are not backticks, they are backslashes. I have fixed it for you.

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 07:59 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/5 "2022-09-10T07:59:41Z")

</div>

oops!..sorry for being so dumb!! 😞

---

<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: [10 September 2022 08:06 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/6 "2022-09-10T08:06:06Z")

</div>

> [@joey\_ind](#):
>
> TypeError: this.tail.search is not a function

Where are you seeing that error and which node is generating it? If it is in the debug pane then it should show the node name or id above it. If you click on that it should take you to the node.

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 08:31 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/7 "2022-09-10T08:31:44Z")

</div>

The debug node is connected to the output of sql node.

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 08:35 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/8 "2022-09-10T08:35:00Z")

</div>

I am also getting a small red triangle on top of the function node. I guess something is wrong with syntax.

---

<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: [10 September 2022 08:37 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/9 "2022-09-10T08:37:43Z")

</div>

Can you show us a screenshot of the error output please? Give the debug node a name first if it has not got a name.  
Also add a debug node showing what is coming out of the function node and show us that.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [10 September 2022 08:50 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/10 "2022-09-10T08:50:31Z")

</div>

You need to use a 'let, var, or const' to define variables. Like this:

```auto
let temp = Number(global.get("rtdpco2")).toFixed(2);
let doorstatus = global.get("dipco2lwr");
let upper_limit = global.get("pco2-up");
let lower_limit = global.get("pco2-low");
msg.topic = "INSERT INTO pco2 (door_status,temp,upper_limit,lower_limit) VALUES(" + doorstatus + "," + temp +"," + upper_limit +"," + lower_limit +");"
return msg;

```

That will get rid of the red triangle ion the function node.

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [10 September 2022 09:16 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/11 "2022-09-10T09:16:09Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/961b2c43cfa63bdc0381ec2ee4a7e35650304658.png)

---

<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: [10 September 2022 09:22 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/12 "2022-09-10T09:22:09Z")

</div>

So it is not from the debug node, it is direct from the SQL node. And the output of the function node?

---

<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: [10 September 2022 09:43 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/13 "2022-09-10T09:43:41Z")

</div>

You have several mistakes in your flow.

1. As @zenofmud points out, you have undeclared variables in your flow
  - NB: The function is utterly pointless here

2. You have the MSSQL-PLUS node set to use the SQL in the UI - BUT - you are attempting to pass a SQL STRING in via `msg.topic`
  - NB: This is the least safe way of doing SQL - you should use parameters in the UI to avoid SQL injection

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e4146af73aaece71874949ae04e3de2be2e9aec.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c4a3416b7d3ea0c9408b6877c96ab5e7174e31cc.png)

Demo flow...

```auto
[{"id":"ee57a1d4778d19aa","type":"MSSQL","z":"49f61d916c8f6022","mssqlCN":"358e71158db79bf8","name":"read","outField":"payload","returnType":0,"throwErrors":1,"query":"INSERT INTO pco2 \r\n (door_status,temp,upper_limit,lower_limit) \r\nVALUES\r\n (@doorstatus, @temp, @upper_limit, @lower_limit)\r\n","modeOpt":"queryMode","modeOptType":"query","queryOpt":"payload","queryOptType":"editor","paramsOpt":"","paramsOptType":"editor","rows":"rows","rowsType":"msg","params":[{"output":false,"name":"door_status","type":"VarChar","valueType":"global","value":"dipco2lwr","options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}},{"output":false,"name":"temp","type":"Float","valueType":"global","value":"rtdpco2","options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}},{"output":false,"name":"upper_limit","type":"Float","valueType":"global","value":"pco2-low","options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}},{"output":false,"name":"lower_limit","type":"Float","valueType":"global","value":"pco2-up","options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}}],"x":1490,"y":100,"wires":[[]]},{"id":"358e71158db79bf8","type":"MSSQL-CN","tdsVersion":"7_4","name":"","server":"localhost","port":"1433","encyption":false,"trustServerCertificate":false,"database":"DATA","useUTC":false,"connectTimeout":"15000","requestTimeout":"15000","cancelTimeout":"5000","pool":"5","parseJSON":false,"enableArithAbort":true}]

```

---

<div class="post-metadata">

### Author: ![joey\_ind](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@joey\_ind](https://discourse.nodered.org/u/joey_ind)
#### Post date: [11 September 2022 13:18 UTC](https://discourse.nodered.org/t/logging-data-in-to-mssql/67539/14 "2022-09-11T13:18:02Z")

</div>

Thanks a lot Steve!!.. that worked!

Just began working with node- red 5 days ago, so struggling a bit with the codes and syntaxes!

Would also like to commend all the responses I received on this forum .I am part of a lot forums but this level of hand-holding is rarely provided!

Happy to be here!! 😀
