# Need to use message TOPIC to do an INSERT into a database

**URL:** <https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608>\
**Category:** General\
**Created:** [2 June 2021 16:15 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608 "2021-06-02T16:15:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [2 June 2021 16:15 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/1 "2021-06-02T16:15:01Z")

</div>

I can do the INSERT just fine, eg

`INSERT INTO EVENTS (EVENTID,OPID,DATETIME,EVENTTYPE,BRANCHID,DEPTID) VALUES (29,9,'2021-05-22 19:48:01',1,1,1);`

but what I'm trying to do now is somehow make the DATETIME (for example) dynamic, so I can change it based on an upstream node.

Does anyone know how I'd use a placeholder in the TOPIC or maybe a function or some other node to insert changing values into the TOPIC?

I'm familiar with how to do all this for payloads, but not topics

thanks!

---

<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:** [2 June 2021 16:26 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/2 "2021-06-02T16:26:01Z")

</div>

Well you could use a `change` node if you set the date to something like 'date\_goes\_here' when you build the topic, in the change node you could use the 'change' option to replace 'date\_goes\_here' with the date you want.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 June 2021 16:29 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/3 "2021-06-02T16:29:58Z")

</div>

Here is an example of adding date time based on a msg variable.

```auto
[{"id":"b2dfac7d.a7a5d","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"date","v":"2021-05-22 19:48:01","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":100,"y":3780,"wires":[["f7f4877e.01e31"]]},{"id":"f7f4877e.01e31","type":"function","z":"c74669a0.6a34f8","name":"","func":"msg.topic = `INSERT INTO EVENTS (EVENTID,OPID,DATETIME,EVENTTYPE,BRANCHID,DEPTID) VALUES (29,9,'${msg.date}',1,1,1)` ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":3780,"wires":[["76c24b1.5e6a534"]]},{"id":"76c24b1.5e6a534","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":3780,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [2 June 2021 22:58 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/4 "2021-06-02T22:58:09Z")

</div>

Brilliant, thanks, so I could just use for e.g` ${msg.payload)` too ? Thank you!

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [15 June 2021 09:13 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/5 "2021-06-15T09:13:11Z")

</div>

As an extension of this question, I'm trying to do two inserts in one transaction but I keep getting SQL errors no matter how I try to format it - any ideas?! Many thanks

```auto
[
    {
        "id": "c9812b6c.13eb68",
        "type": "inject",
        "z": "447a4ba3.9fb214",
        "name": "set invoice amount",
        "topic": "update TRANS set AMOUNT = '39.71' where TRANS = '99887000038'",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 680,
        "wires": [
            [
                "e543c4d.e890b38"
            ]
        ]
    },
    {
        "id": "eb5d8539.ded2b8",
        "type": "debug",
        "z": "447a4ba3.9fb214",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 590,
        "y": 660,
        "wires": []
    },
    {
        "id": "e543c4d.e890b38",
        "type": "firebird-db",
        "z": "447a4ba3.9fb214",
        "firebirddb": "a94792eb.18f8d",
        "requesttype": "query",
        "name": "",
        "x": 400,
        "y": 760,
        "wires": [
            [
                "eb5d8539.ded2b8"
            ]
        ]
    },
    {
        "id": "a94792eb.18f8d",
        "type": "firebird-database",
        "z": "",
        "host": "192.168.1.130",
        "port": "3040",
        "db": "C:\\data\\CHECK.FDB"
    }
]

```

---

<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:** [15 June 2021 14:14 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/6 "2021-06-15T14:14:36Z")

</div>

> [@dclear](#):
>
> I'm trying to do two inserts in one transaction

Your code is showing you do an update:  
`update TRANS set AMOUNT = '39.71' where TRANS = '99887000038'"`  
not an insert.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [15 June 2021 14:21 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/7 "2021-06-15T14:21:42Z")

</div>

Sorry I'm not using the right term. 🙂 But I guess the question is the same whether it's an insert or an update. At the moment I've fudged it by doing an inject which feeds into a single change node which feeds into two function nodes, but I think the function nodes ought to be combined

---

<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:** [15 June 2021 14:35 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/8 "2021-06-15T14:35:15Z")

</div>

I know nothing about the firebirddb and you haven't provided the name of the contrib node you are using so it is hard to say what it will flow.

You first have to research and determine if doing multiple inserts or updates is allowed by the firebird api, then look at the node and see what it allows you to do.

If it is something you really need then you should go to the GitHub issues page for the node (lookup the node in the 'FLows' tab, then follow the GitHub link) and open an issue with the author of the node.

---

<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:** [14 August 2021 14:35 UTC](https://discourse.nodered.org/t/need-to-use-message-topic-to-do-an-insert-into-a-database/46608/9 "2021-08-14T14:35:27Z")

</div>

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