markgargan
Programmer
Hey folks,
I've created a cursor before for a select statement.
However the sql in the select statement never had to have a 'where' clause with it and as such I'm having terrible trouble
escaping the single-quote (') character
e.g. Before :-
SET countSQL ='SELECT Count(*) FROM ' || vTableName;
This is fine as it creates a string variable called countSQL
with the following SQL
SELECT Count(*) from myTable
for example
however now I need to perform the following sql
set cardSQL ='SELECT CARD from syscat.tables where tabname=' || vTableName;
but this produces
SELECT CARD from syscat.tables where tabname=myTable
however this fails in the procedure because there's no single-quotes surrounding myTable
I've tried escaping this using the backslash (\) char but it won't even compile for me.
i.e.
set cardSQL ='SELECT CARD from syscat.tables where tabname=\' ' || vTableName || ' \' ';
but all I get is the following error
DB2ADMIN.DBCHANGE: 60: [IBM][CLI Driver][DB2/NT] SQL0007N The character "\" following "|| vTableName || '" is not valid. LINE NUMBER=60. SQLSTATE=42601
Does anyone know how to escape a string in a stored procedure?
Thanks,
Mark
I've created a cursor before for a select statement.
However the sql in the select statement never had to have a 'where' clause with it and as such I'm having terrible trouble
escaping the single-quote (') character
e.g. Before :-
SET countSQL ='SELECT Count(*) FROM ' || vTableName;
This is fine as it creates a string variable called countSQL
with the following SQL
SELECT Count(*) from myTable
for example
however now I need to perform the following sql
set cardSQL ='SELECT CARD from syscat.tables where tabname=' || vTableName;
but this produces
SELECT CARD from syscat.tables where tabname=myTable
however this fails in the procedure because there's no single-quotes surrounding myTable
I've tried escaping this using the backslash (\) char but it won't even compile for me.
i.e.
set cardSQL ='SELECT CARD from syscat.tables where tabname=\' ' || vTableName || ' \' ';
but all I get is the following error
DB2ADMIN.DBCHANGE: 60: [IBM][CLI Driver][DB2/NT] SQL0007N The character "\" following "|| vTableName || '" is not valid. LINE NUMBER=60. SQLSTATE=42601
Does anyone know how to escape a string in a stored procedure?
Thanks,
Mark