问题
Running Pro*C on Oracle 10g.
I am looking to do a subquery within an insert statement values clause. This sql query is fully valid and runs within TOAD with no problems, but Pro*C fails to parse the query.
EXEC SQL INSERT INTO TARGET_ATTACHMENT
(
TARGET_ID
FILENAME
)
VALUES (
:targetID,
( SELECT CREATED_FLAG from TARGET t where t.TARGET_ID = :targetID ) || '.tif'
)
If I remove:
( SELECT (CREATED_FLAG || DISPLAY_ID) from TARGET t where t.TARGET_ID = :targetID ) ||**".
The Pro*C compiler works and everything compiles and runs as expected.
If I DO NOT remove: The Pro*C compiler throws a syntax error.
1>Syntax error at line 128, column 12, file d:\SVN\...\TA.pc:
1>Error at line 128, column 12 in file d:\SVN\...
1>...\TA.pc
1> ( select CREATED_FLAG from target t where t.TARGET_ID = :targetID )
1>...........1
1>PCC-S-02201, Encountered the symbol "CREATED_FLAG" when expecting one of the fol
1>lowing:
1> ( ) * + - / . @ | at, day, hour, minute, month, second, year,
This is a problem, as I expect Pro*C to be able to compile subquerys within a values caluse:
ie.
INSERT into table1 (col1) values ( (select t2.singleCol from table2 t2 where t2.priKey = :priKey) )
Is this expected behaviour of Pro*C? or Should it support subqueries within the values clause?
回答1:
Possibly change the subquery to:
( SELECT CREATED_FLAG || '.tif' from TARGET t where t.TARGET_ID = :targetID )
I dont think I have ever seen something appended to a subquery the way you were attempting.
回答2:
The amount of SQL the Pro*C preprocessor is able to parse in static SQL statements is quite limited. For example it can't even parse explicit inner joiner
/outer left join
etc. notation.
As a workaround you can just prepare a dynamic SQL statement and execute it - even if your SQL statement is not really dynamic.
回答3:
The code you have posted is logically identical to this:
INSERT INTO TARGET_ATTACHMENT
( TARGET_ID , FILENAME )
select :targetID, CREATED_FLAG|| '.tif'
from TARGET t
where t.TARGET_ID = :targetID )
Is there a particular reason why you need to use scalar cursors in a VALUES clause?
来源:https://stackoverflow.com/questions/12711271/oracle-proc-insert-into-values-select