[archive] SQL 'Order By' problem

[Migrated content. Thread originally posted on 30 July 2009]

Hello
We have the following code embedded in a cobol program.
We want the returned records to be returned sorted in either ascending or descending order depending on the variable ws-sort-flag.
We get the following error message when we run it:
"a constant expression was encountered in the Order By list, position 1"
When we run the identical code in the SQL Managemant Console it works fine.
Does anyone have any ideas or different code that might work?
Thanks!
EXEC SQL
DECLARE ConvCode1 CURSOR FOR
SELECT CST_RectCode, CST_RectFmma, CST_RectName,
CST_RectLoc
FROM tbl_Customer WITH (NOLOCK)
WHERE CST_IDNUMBER = :WS-Handler-IDNumber
ORDER BY
CASE :WS-SORT-FLAG
WHEN 0 THEN
CST_RectCode CONVERT(CHAR,CST_RectFmma)
END ASC,
CASE :WS-SORT-FLAG
WHEN 1 THEN
CST_RectCode CONVERT(CHAR,CST_RectFmma)
END DESC
FOR READ ONLY
END-EXEC