Not to get into a discussion about SQL injection and sanitizing and etc...
For various reasons, I am doing the unspeakable and building and executing a dynamic SQL query WITHOUT using query params.
So... I'm building a list of columns and a list of values, and then I have the following code:
insertRecord.setSql("INSERT INTO MyTable(" & columns & ") VALUES(" & values & ")");
This works properly for most records, but for a few records one of the string field values has an apostrophe or single quote:
'SHERIFF'S OFFICE'
This is giving me the SQL error: Incorrect syntax near 'S'.
I have tried using preserve single quotes in several ways and it doesn't help:
insertRecord.setSql("INSERT INTO MyTable(" & columns & ") VALUES(" & preserveSingleQuotes(values) & ")");
OR
insertRecord.setSql(PreserveSingleQuotes("INSERT INTO MyTable(" & columns & ") VALUES(" & values & ")"));
Is there any way to escape all the apostrophes, or otherwise format my sql string properly, other than using a ReplaceNoCase(colValue, "'", "''")
on each value individually as I'm building the string (which does work)?