tags:

views:

138

answers:

1

Please tell me whats wrong with the query. I am using Pro C.

EXEC SQL SELECT 1
        INTO    :db_count
        FROM    sachin t
        WHERE   t.serialno =   :serial_no
        AND     t.amount = (:db_inamount - (SELECT NVL(overrun_amount,0)
                                            FROM  sunny tovrun
                                            WHERE tovrun.serialno = :serial_no
                                            AND   tovrun.timestamp = t.timestamp
                                            AND   rownum < 2)
                            )
        AND     t.request_code = 11
        AND     t.reason_code = 0
        AND     t.reversed    = 0
        AND rownum < 2;

And getting the compilation errors

Syntax error at line 4487, column 42, file at_txnlog.pc:
Error at line 4487, column 42 in file at_txnlog.pc
        AND             t.amount = (:db_inamount - (SELECT NVL(overrun_amount,0)

.........................................1
PCC-S-02201, Encountered the symbol "NVL" when expecting one of the following:

   ( ) * + - / . @ | at, day, hour, minute, month, second, year,
The symbol "(" was substituted for "NVL" to continue.

Syntax error at line 4488, column 14, file at_txnlog.pc:
Error at line 4488, column 14 in file at_txnlog.pc
A: 

Use:

 AND t.amount = (SELECT :db_amount - NVL(overrun_amount, 0) ...

It's a standard computed column, where the value is calculated prior to the comparison to the t.amount value for equality.

OMG Ponies
Yes thats true, but I want to know why AND t.amount = (SELECT :db_amount - NVL(overrun_amount, 0) worked and (:db_inamount - (SELECT NVL(overrun_amount,0) from table ) doesnot.
Sachin Chourasiya
The latter is a scalar subquery expression, which was introduced back in 8i. But those 'modern' features aren't understood by the SQL parser in the pre-compiler. OMG's solution is a simpler subquery syntax supported since wayback.
Gary
Thanks Gray I got it
Sachin Chourasiya