Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem using 'execute immediate' in PL SQL Procedure

Tags:

sql

oracle

plsql

I am using the following code in a PL SQL procedure:

execute immediate 'select count(distinct item_type) into counter_variable
from items where ' || field_name || ' is not null'

Here,

counter_variable is declared in the declaration section of the procedure field_name is the IN parameter to the PL SQL procedure and the values passed will be column names from the 'items' table

The error that I get is 'Invalid SQL Statement' and I am not able to figure out the reason. Any ideas?

Thanks

like image 895
tusay Avatar asked Jun 07 '11 11:06

tusay


1 Answers

The into clause is PL/SQL and not valid in an SQL statement. Try this:

execute immediate 'select count(distinct item_type) 
from items where ' || field_name || ' is not null' into counter_variable
like image 173
Klas Lindbäck Avatar answered Sep 27 '22 20:09

Klas Lindbäck