You do not need a function node to do that. A template would work and use less resources.
e,g
[{"id":"745b7a4ec367a4af","type":"inject","z":"bac113592b6fd1c6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"sensor_serial_no","v":"1234abc","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test_topic","payload":"test_payload","payloadType":"str","x":120,"y":360,"wires":[["f7f2b86dc46f6310"]]},{"id":"f7f2b86dc46f6310","type":"template","z":"bac113592b6fd1c6","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"INSERT INTO pi_sensor(\nsensor_serial_no,\ntopic,\npayload\n) VALUES (\n'{{sensor_serial_no}}',\n'{{topic}}',\n'{{payload}}'\n)","output":"str","x":320,"y":360,"wires":[["790447ce9e31f09b"]]},{"id":"790447ce9e31f09b","type":"debug","z":"bac113592b6fd1c6","name":"debug 324","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":360,"wires":[]}]
But if you want a function node to do it, you can use a Template String
[{"id":"745b7a4ec367a4af","type":"inject","z":"bac113592b6fd1c6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"sensor_serial_no","v":"1234abc","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test_topic","payload":"test_payload","payloadType":"str","x":120,"y":360,"wires":[["dda9731b9c09bd4d"]]},{"id":"dda9731b9c09bd4d","type":"function","z":"bac113592b6fd1c6","name":"function 21","func":"msg.topic = `INSERT INTO pi_sensor(\nsensor_serial_no,\ntopic,\npayload\n) VALUES (\n'${msg.sensor_serial_no}',\n'${msg.topic}',\n'${msg.payload}'\n)`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":360,"wires":[["790447ce9e31f09b"]]},{"id":"790447ce9e31f09b","type":"debug","z":"bac113592b6fd1c6","name":"debug 324","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":360,"wires":[]}]
You should also sanitize all variables used in the query , to prevent any sql injections.