Variables are not taken in a string

Here is what I'm trying to do.
A query embedded with a unix variable is stored in the database table. When it is pulled out, it has a variable in it and it needs to be replaced by the variable passed to the script below.

1. select cast(cmd_string as varchar(1000)) from mytable where tabname='mytable'
Output from the query:
select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
2. Now this query is passed to a variable
lowsql="${abovequery}"
echo lowsql:
 select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
3. Now trying to replace $lowvalue with the actual value:
function runq {
lowvalue=$1
echo lowvalue $lowvalue before
echo "lowsql before $lowsql"
p=`echo "$lowsql"`
echo p: $p
}
runq 201302
  1. Output from this function:
    ./makeq.ksh 201302
    lowvalue 201302 before
    lowsql before select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
    p: select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur

However, $lowvalue is not being replaced by the actual value 201302.

Anyone has any suggestion or idea why?

Thanks.

Sheila

Try the deprecated and dangerous eval !