# Read SQL data and write to PLC

**URL:** <https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278>\
**Category:** General\
**Created:** [5 April 2023 14:45 UTC](https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278 "2023-04-05T14:45:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![machadof11](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@machadof11](https://discourse.nodered.org/u/machadof11)\
**Post date:** [5 April 2023 14:45 UTC](https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278/1 "2023-04-05T14:45:22Z")

</div>

Hi,

I would like to read a value in SQL server and write it to a Rockwell PLC.  
With "Inject" node it was possible to write, now I would like to reade (SQL) an write to PLC.  
Thanks.

---

<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:** [5 April 2023 15:20 UTC](https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278/2 "2023-04-05T15:20:14Z")

</div>

Put a SQL Node (I recommend node-red-contrib-mssql-plus) between the inject and the and the PLC node.

Use a change node or function node to format the SQL data into the correct payload format.

---

<div class="post-metadata">

**Author:** ![machadof11](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@machadof11](https://discourse.nodered.org/u/machadof11)\
**Post date:** [6 April 2023 14:05 UTC](https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278/3 "2023-04-06T14:05:00Z")

</div>

Hi,

Thank you Steve-Mcl, I tried to reproduce what you said but I think I did some wrong configuration in the change node.

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

```auto
[
    {
        "id": "e441ea33b0e8edbf",
        "type": "tab",
        "label": "DnA_Prediction",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c34001de6df9fa2b",
        "type": "eth-ip out",
        "z": "e441ea33b0e8edbf",
        "endpoint": "31600212ce3ff55f",
        "variable": "TesteDnA",
        "program": "",
        "name": "TesteDnA1",
        "x": 310,
        "y": 240,
        "wires": []
    },
    {
        "id": "e46b4da53d6e058b",
        "type": "MSSQL",
        "z": "e441ea33b0e8edbf",
        "mssqlCN": "30fec03c812ee923",
        "name": "DnA_SP",
        "query": "SELECT TOP 1 Afinagem \nFROM [DnA].[dbo].[Banco_Secador_SP]",
        "outField": "payload",
        "x": 260,
        "y": 360,
        "wires": [
            [
                "a96578912e68c0cb"
            ]
        ]
    },
    {
        "id": "e4f47ce9a8bfe538",
        "type": "debug",
        "z": "e441ea33b0e8edbf",
        "name": "debug 8",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 220,
        "y": 620,
        "wires": []
    },
    {
        "id": "a96578912e68c0cb",
        "type": "change",
        "z": "e441ea33b0e8edbf",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "payload[0].Afinagem",
                "fromt": "str",
                "to": "",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 230,
        "y": 480,
        "wires": [
            [
                "e4f47ce9a8bfe538"
            ]
        ]
    },
    {
        "id": "79d3c146a55e631f",
        "type": "inject",
        "z": "e441ea33b0e8edbf",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "SELECT TOP 1 Afinagem FROM [DnA].[dbo].[Banco_Secador_SP]",
        "payloadType": "str",
        "x": 90,
        "y": 300,
        "wires": [
            [
                "e46b4da53d6e058b"
            ]
        ]
    },
    {
        "id": "31600212ce3ff55f",
        "type": "eth-ip endpoint",
        "z": "e441ea33b0e8edbf",
        "address": "192.168.155.28",
        "slot": "0",
        "cycletime": "5000",
        "name": "TesteDna",
        "vartable": {
            "": {
                "TesteDnA": {
                    "type": "REAL"
                }
            }
        }
    },
    {
        "id": "30fec03c812ee923",
        "type": "MSSQL-CN",
        "name": "Dryer_DnA_SP",
        "server": "KPISERVER\\SSA",
        "encyption": false,
        "database": "Dna"
    }
]

```

---

<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:** [5 June 2023 14:05 UTC](https://discourse.nodered.org/t/read-sql-data-and-write-to-plc/77278/4 "2023-06-05T14:05:39Z")

</div>

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