Let's take it one step at a time. First, static code that works:
SQL> declare
2 foo1 number:=111;
3 foo2 number:=222;
4 begin
5 dbms_output.put_line(foo1);
6 end;
7 /
111
Now, if you want to EXECUTE IMMEDIATE some text, you have to put all the code in the text:
SQL> declare
2 l_code_block varchar2(4000) := '
3 declare
4 foo1 number:=111;
5 foo2 number:=222;
6 begin
7 dbms_output.put_line(foo2);
8 end;
9 ';
10 begin
11 execute immediate l_code_block;
12 end;
13 /
222
Next step: if we want to change a value dynamically, we can and should use a "bind variable".
SQL> declare
2 l_code_block varchar2(4000) := '
3 declare
4 foo1 number:=111;
5 foo2 number:=222;
6 begin
7 dbms_output.put_line(:n);
8 end;
9 ';
10 begin
11 execute immediate l_code_block using 1;
12 end;
13 /
1
But if we try to use a bind variable to change the code, it doesn't work.
SQL> declare
2 l_code_block varchar2(4000) := '
3 declare
4 foo1 number:=111;
5 foo2 number:=222;
6 begin
7 dbms_output.put_line(foo:n);
8 end;
9 ';
10 begin
11 execute immediate l_code_block using 1;
12 end;
13 /
...
Error report -
ORA-06550: line 6, column 25:
PLS-00103: Encountered the symbol "" when expecting one of the following
...
So if we want to change the code dynamically, we have to do a REPLACE on the text.
SQL> declare
2 l_code_block varchar2(4000) := '
3 declare
4 foo1 number:=111;
5 foo2 number:=222;
6 begin
7 dbms_output.put_line(foo#N#);
8 end;
9 ';
10 begin
11 execute immediate replace(l_code_block,'#N#',1);
12 end;
13 /
111
Finally, here is your loop:
SQL> declare
2 l_code_block varchar2(4000) := '
3 declare
4 foo1 number:=111;
5 foo2 number:=222;
6 begin
7 dbms_output.put_line(foo#N#);
8 end;
9 ';
10 begin
11 for i in 1..2 loop
12 execute immediate replace(l_code_block,'#N#',i);
13 end loop;
14 end;
15 /
111
222
Please understand, I am trying to answer your question as asked. This "dynamic" approach should be avoided whenever possible, and it is almost always possible. We would have to back up to the business requirement in order to recommend the most appropriate technique.
Best regards,
Stew Ashton