Extracting a value from an MS-SQL database

Hello everyone.

Assuming I have the following SQL table (ignore the fact that the picture is from excel)-

And I receive the value 'Jerry' in the payload, how can I extract his phone number from the MS-SQL table?

Much thanks,

Something like SQL Server SELECT - Querying Data from a Single Table ?

Sort of, couldn't quite yet form the picture of how it should look like in a flow.
If I am constantly receiving people's names in payloads and I want to use that payload value in order to extract their phone numbers.

What a lot of people do is use a template node to create the query string eg like "SELECT foo FROM mytable WHERE name='{{payload}}';" then pass that to the node.

Thanks! :slight_smile:

Please be aware that this can be susceptible to SQL Injection.

For safety, it is always better to use Stored Procedures or Parameters

Using node-red-contrib-mssql-plus...

1 Like

This is amazing, thanks.
However, I receive the output as an object inside an array. How can I receive it only as an object?
It looks as so-

I cant seem to be able to call this object out with msg.payload.token.

No, because as the debug output states, msg.payload is an array.

The result of a SQL query is ALWAYS an array - because it has to represent a set of database rows - where each array element is a database row.

If you want to access the 1st row, then you would access msg.payload[0].token


There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.


Much thanks. I'll make sure to view the attached documentation you've mentioned.

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