![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay |
#3
| |||
| |||
|
|
I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay |
#4
| |||
| |||
|
|
On Mar 26, 11:48*pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote: I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay Basically you have to replace each occurance of a single quote in variable name2 by two single quotes. As I know you are programming with OpenROAD here is an example: SQLStmt = 'UPDATE pepl SET name2 = ' + * * ReplaceText(text = name2, match='''', replacement = '''''')+ ' WHERE ....'; This uses 4GL Procedure "ReplaceText" having the following code: procedure ReplaceText( * * text = varchar(2000) not null, * * match = varchar(256) not null, * * replacement = varchar(256) not null )= DECLARE * * pos = integer not null; * * v = varchar(2000) not null; * * newtext = varchar(2000) not null; ENDDECLARE BEGIN * * v = text; * * while (length(v) > 0) do * * * * pos = locate(v, match); * * * * newtext = newtext + left(v, pos-1); * * * * if pos <= length(v) then * * * * * * newtext = newtext+replacement; * * * * endif; * * * * v = shift(v, -(pos+length(match)-1)); * * endwhile; * * return newtext; END HTH, Bodo.- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
|
On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote: I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay Basically you have to replace each occurance of a single quote in variable name2 by two single quotes. As I know you are programming with OpenROAD here is an example: SQLStmt = 'UPDATE pepl SET name2 = ' + ReplaceText(text = name2, match='''', replacement = '''''') + ' WHERE ....'; This uses 4GL Procedure "ReplaceText" having the following code: procedure ReplaceText( text = varchar(2000) not null, match = varchar(256) not null, replacement = varchar(256) not null )= DECLARE pos = integer not null; v = varchar(2000) not null; newtext = varchar(2000) not null; ENDDECLARE BEGIN v = text; while (length(v) > 0) do pos = locate(v, match); newtext = newtext + left(v, pos-1); if pos <= length(v) then newtext = newtext+replacement; endif; v = shift(v, -(pos+length(match)-1)); endwhile; return newtext; END HTH, Bodo. |
#6
| |||
| |||
|
|
This is fine if I know only pepl.name2 can have single quotes and only need to code explicitly for that column However any varchar could have a quote in it - i.e. descriptions, addresses etc Every SQL statement is being executed in C from a common handler OpenROADprocedure so ideally what I want is a routine in that procedure that takes any SQL statement string, does some some semantic checking, adding the extra quote where deemed necessary My first attempt counted the number of quotes delimited by a comma or '=' Where I can count up to 3 quotes I escape the 2nd so 'O'MALLEY' becomes 'O''MALLEY' the problem then was that 'O'MALLEY, SEAMUS' doesn't work because my algorithm doesn't realise that the comma is part of the string ... I guess I should then delimit by ' followed by any number of spaces followed by , I was just wondering if anyone had had this problem before and had a working solution before I spend any more time on this The alternative is to have to scan the existing data in every char columnand narrow it down to a list of possible problem columns and explicitly code them ... "Bodo" <Bodo.Bergmann (AT) ingres (DOT) com> wrote On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote: I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay Basically you have to replace each occurance of a single quote in variable name2 by two single quotes. As I know you are programming with OpenROAD here is an example: SQLStmt = 'UPDATE pepl SET name2 = ' + ReplaceText(text = name2, match='''', replacement = '''''') + ' WHERE ....'; This uses 4GL Procedure "ReplaceText" having the following code: procedure ReplaceText( text = varchar(2000) not null, match = varchar(256) not null, replacement = varchar(256) not null )= DECLARE pos = integer not null; v = varchar(2000) not null; newtext = varchar(2000) not null; ENDDECLARE BEGIN v = text; while (length(v) > 0) do pos = locate(v, match); newtext = newtext + left(v, pos-1); if pos <= length(v) then newtext = newtext+replacement; endif; v = shift(v, -(pos+length(match)-1)); endwhile; return newtext; END HTH, Bodo. |
#7
| |||
| |||
|
|
I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay |
#8
| |||
| |||
|
|
This is fine if I know only pepl.name2 can have single quotes and only need to code explicitly for that column However any varchar could have a quote in it - i.e. descriptions, addresses etc Every SQL statement is being executed in C from a common handler OpenROADprocedure so ideally what I want is a routine in that procedure that takes any SQL statement string, does some some semantic checking, adding the extra quote where deemed necessary My first attempt counted the number of quotes delimited by a comma or '=' Where I can count up to 3 quotes I escape the 2nd so 'O'MALLEY' becomes 'O''MALLEY' the problem then was that 'O'MALLEY, SEAMUS' doesn't work because my algorithm doesn't realise that the comma is part of the string ... I guess I should then delimit by ' followed by any number of spaces followed by , I was just wondering if anyone had had this problem before and had a working solution before I spend any more time on this The alternative is to have to scan the existing data in every char columnand narrow it down to a list of possible problem columns and explicitly code them ... "Bodo" <Bodo.Bergm... (AT) ingres (DOT) com> wrote in messagenews:b465552a-df4e-420b-adf6-37a19f2aa7f2 (AT) n17g2000vba (DOT) googlegroups.com... On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote: I have an ESQLC program that handles SQL statements as strings. It executes all non-SELECT statements with an EXEC SQL EXECUTE IMMEDIATE :SQLStmt statement. This works fine except for strings that include single quotes e.g. name2 = 'O''MALLEY'; SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....' results in 'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....' which will fail Has anyone out there ever come up with an algorithm for processing a string like this to escape the correct single quote that will work with ANY sql statement ? Colin Hay Basically you have to replace each occurance of a single quote in variable name2 by two single quotes. As I know you are programming with OpenROAD here is an example: SQLStmt = 'UPDATE pepl SET name2 = ' + * *ReplaceText(text = name2, match='''', replacement = '''''') + ' WHERE ....'; This uses 4GL Procedure "ReplaceText" having the following code: procedure ReplaceText( * *text = varchar(2000) not null, * *match = varchar(256) not null, * *replacement = varchar(256) not null )= DECLARE * *pos = integer not null; * *v = varchar(2000) not null; * *newtext = varchar(2000) not null; ENDDECLARE BEGIN * *v = text; * *while (length(v) > 0) do pos = locate(v, match); newtext = newtext + left(v, pos-1); if pos <= length(v) then * * newtext = newtext+replacement; endif; v = shift(v, -(pos+length(match)-1)); * *endwhile; * *return newtext; END HTH, Bodo.- Hide quoted text - - Show quoted text - |
![]() |
| Thread Tools | |
| Display Modes | |
| |