Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic IF statement in PL/SQL (2 threads merged by bb) (Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit, HP-UX)
Dynamic IF statement in PL/SQL (2 threads merged by bb) [message #485618] Thu, 09 December 2010 02:27 Go to next message
rs1969
Messages: 24
Registered: December 2007
Location: UK
Junior Member
I am trying to generate some dynamic IF statements from an XML string. Please see sample code which is giving an error that variable 'v_emp_id' must be declared. Please let me know if this is possible. Thanks in advance.

SET SERVEROUTPUT ON
DECLARE
   v_field_name    VARCHAR2 (50) := 'v_emp_id';
   v_compare       VARCHAR2 (50) := '=';
   v_value         VARCHAR2 (50) := '1234';
   v_emp_id        VARCHAR2 (50) := '1234';
   v_check         VARCHAR2 (5)  := 'FALSE';
   v_if_statement  VARCHAR2 (1000);
   v_sql_statement VARCHAR2 (1000);
BEGIN
   v_if_statement := 'IF '||v_field_name||' '||v_compare||' '||''''||v_value||''''||' '||
                     'THEN :v_check := ''TRUE'';'||' '||
                     'END IF;';
   
   v_sql_statement := 'BEGIN '||v_if_statement||' END;';
   
   --Remove any special characters
   v_sql_statement := REPLACE(REPLACE(REPLACE(v_sql_statement,CHR(10),' '),CHR(13),' '),CHR(9),' ');
   
   dbms_output.put_line('v_sql_statement : '||v_sql_statement);
   
   EXECUTE IMMEDIATE v_sql_statement USING v_check;
   
   dbms_output.put_line('v_check : '||v_check);
END;
/

DECLARE
*
ERROR at line 1:
ORA-06550: line 1, column 10:
PLS-00201: identifier 'V_EMP_ID' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
ORA-06512: at line 21
Re: Dynamic IF statement in PL/SQL [message #485620 is a reply to message #485618] Thu, 09 December 2010 02:50 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
Put a colon in front of v_emp_id in your v_if_statement, then use "in v_emp_id, out v_check" after your execute immediate, as shown below.

SCOTT@orcl_11gR2> SET SERVEROUTPUT ON
SCOTT@orcl_11gR2> DECLARE
  2  	v_field_name	VARCHAR2 (50) := 'v_emp_id';
  3  	v_compare	VARCHAR2 (50) := '=';
  4  	v_value 	VARCHAR2 (50) := '1234';
  5  	v_emp_id	VARCHAR2 (50) := '1234';
  6  	v_check 	VARCHAR2 (5)  := 'FALSE';
  7  	v_if_statement	VARCHAR2 (1000);
  8  	v_sql_statement VARCHAR2 (1000);
  9  BEGIN
 10  	v_if_statement := 'IF :'||v_field_name||' '||v_compare||' '||''''||v_value||''''||' '||
 11  			  'THEN :v_check := ''TRUE'';'||' '||
 12  			  'END IF;';
 13  
 14  	v_sql_statement := 'BEGIN '||v_if_statement||' END;';
 15  
 16  	--Remove any special characters
 17  	v_sql_statement := REPLACE(REPLACE(REPLACE(v_sql_statement,CHR(10),' '),CHR(13),' '),CHR(9),' ');
 18  
 19  	dbms_output.put_line('v_sql_statement : '||v_sql_statement);
 20  
 21  	EXECUTE IMMEDIATE v_sql_statement USING IN v_emp_id, OUT v_check;
 22  
 23  	dbms_output.put_line('v_check : '||v_check);
 24  END;
 25  /
v_sql_statement : BEGIN IF :v_emp_id = '1234' THEN :v_check := 'TRUE'; END IF;
END;
v_check : TRUE

PL/SQL procedure successfully completed.

SCOTT@orcl_11gR2>

Re: Dynamic IF statement in PL/SQL [message #485622 is a reply to message #485620] Thu, 09 December 2010 03:02 Go to previous messageGo to next message
rs1969
Messages: 24
Registered: December 2007
Location: UK
Junior Member
Wow, that was quick !! Thanks a lot Barbara
Re: Dynamic IF statement in PL/SQL [message #485625 is a reply to message #485622] Thu, 09 December 2010 03:24 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Just note that V_EMP_ID variable was hardcoded in USING clause of EXECUTE IMMEDIATE statement. The value of V_FIELD_NAME is irrelevant here (well, it only has to be a valid identifier).

Referencing local variable dynamically this way is not possible - you will be always out of scope. There are two possible workarounds here: create a collection of variables (TABLE OF) and reference it by position/index. Other way would be using variables in package specification, as described e.g. in this thread on AskTom: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:227413938857#2973344838545

Variables are hardcoded in DECLARE section anyway, so why not access them directly. What are you really trying to achieve?

[Edit: added link to AskTom thread]

[Updated on: Thu, 09 December 2010 03:31]

Report message to a moderator

Re: Dynamic IF statement in PL/SQL [message #485635 is a reply to message #485625] Thu, 09 December 2010 04:00 Go to previous messageGo to next message
rs1969
Messages: 24
Registered: December 2007
Location: UK
Junior Member
You are right. The variable is hard-coded in my example. Actually I would have liked to pass v_field_name in USING clause, so that it's (value) gets substituted in the IF statement.

e.g. IF :v_field_name = v_value needs to be translated to
IF v_emp_id = '1234'

How can I achieve this? Please let me know if you want me to post the entire code.

Thanks

[Updated on: Thu, 09 December 2010 04:02]

Report message to a moderator

Re: Dynamic IF statement in PL/SQL [message #485655 is a reply to message #485635] Thu, 09 December 2010 05:17 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
rs1969 wrote on Thu, 09 December 2010 11:00
How can I achieve this? Please let me know if you want me to post the entire code.

If they are declared as local variables, you cannot. Period.
There are two workarounds listed in my answer, the second one with the code in enclosed link. You might try to implement them.

But, as the variables are statically declared, they were (probably) statically filled with some value, why should they be suddenly dynamically accessed for checking their values?
Re: Dynamic IF statement in PL/SQL [message #485662 is a reply to message #485655] Thu, 09 December 2010 05:51 Go to previous message
rs1969
Messages: 24
Registered: December 2007
Location: UK
Junior Member
Thanks for providing the Ask Tom link which helped in resolving my problem. I am now using global package variables.

In my example, the values were static. But in the actual implementation the fields, conditions etc. are stored in XML format. Any changes in the future would require change to XML stored in the database without recompiling the code.

Previous Topic: What is Backend process of Oracle Database?
Next Topic: UNION, ALL & Intersection
Goto Forum:
  


Current Time: Wed Jul 16 11:37:21 CDT 2025