5

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

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget: If field_name is supplied by a user it has to be checked for SQL injection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.