1

I have a function, and I want to determine the name of the column in run time. For this I am passing one variable as an argument, like column_name.

Below is the code with the function:

l_column_name as varchar2(100)

Begin 

If(column_name='emp_name')
  Then 
  l_column_name:=EMPLOYEE.EMP_NAME
End If;

   begin
 select l_column_name from employee
end;

In above code, l_column_name:=EMPLOYEE.EMP_NAME is giving the error

Not allowed in this context.

Any help is much appreicated.

Regards, Chaitu

1 Answer 1

4

As the error says, you can not do this.

You need to look into using the PL/SQL Execute Immediate: http://docs.oracle.com/cd/B14117_01/appdev.101/b10807/13_elems017.htm

declare
  l_column_name as varchar2(100);
  l_column_results as VARCHAR2(100);
begin
  if (column_name = 'emp_name') then
     l_column_name := 'EMPLOYEE.EMP_NAME';
  end if;
  query := 'SELECT ' || l_column_name || ' from employeee';
  EXECUTE IMMEDIATE query INTO l_column_results;
end;
Sign up to request clarification or add additional context in comments.

1 Comment

yes, it's the only way to get results from the query back into your pl/sql code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.