Currently, I am developing a SQL query using Javascript where I pass values. If any value is undefined, I want it to be passed as null
. This is how my code looks:
const sql = `INSERT INTO MYTABLE(
COLUMN1,
COLUMN2
)
VALUES(
${param.value1 ?? null},
${param.value2 ?? null}
)`;
Although the null check functions correctly, the database rejects values that are defined because they are not enclosed in single quotes. For example:
INSERT INTO MYTABLE(
COLUMN1,
COLUMN2
)
VALUES(
test,
null
)
The database requires me to wrap test
in single quotes like 'test'
. However, I am struggling to find an effortless method to achieve this. How can I automatically add single quotes around my values if they are not undefined?
Thank you.