Oracle PLSQL in CFQUERY

Sometimes, you may want to execute an anonymous PL/SQL block directly from within the CFQUERY tag instead of calling a stored procedure or function. So you write this (simplified) code in your ColdFusion template:

<cfquery name="q" datasource="yourDSN">
    declare
        x number;
    begin
        x := 0;
    end;
</cfquery>

Only to get the following error when executing the above code:

Error Executing Database Query.  
[Macromedia][Oracle JDBC Driver][Oracle]ORA-06550: 
line 1, column 8: PLS-00103: Encountered the symbol "" 
when expecting one of the following: begin function 
package pragma procedure ...

The workaround is to put the PL/SQL code in a ColdFusion variable and use the variable inside the CFQUERY tag:

<cfset variables.plsql = "
    declare
        x number;
    begin
        x := 0;
    end;
"
>
<cfquery name="q" datasource="yourDSN">
    #variables.plsql#
</cfquery>

The above works like a charm!


Possibly related:


Tagged , | Post a Comment