1

I am trying to use execute immediate as a special requirement for assigning the value of a variable. I am using the following code and getting the exception as below.

declare
  lv_kyc_main_GBL KYC_GBL_MAIN.KYC_MAIN%rowtype;
  l_str varchar2(400);

  a number(10);
begin
  select *
    into lv_kyc_main_GBL
    from KYC_GBL_MAIN.KYC_MAIN
   where rownum = 1;

  -- l_str:='lv_kyc_main_GBL.legal_name'||':='||'''TEST''';

  l_str := 'lv_kyc_main_GBL.legal_name := ''TEST''';
  dbms_output.put_line(l_str);

  execute immediate (l_str);
end;
/

The exception I'm getting is:

anonymous block completed
lv_kyc_main_GBL.legal_name := 'TEST'

Error starting at line 4 in command:

declare
  lv_kyc_main_GBL KYC_GBL_MAIN.KYC_MAIN%rowtype;
  l_str varchar2(400);

  a number(10);
begin
  select *
    into lv_kyc_main_GBL
    from KYC_GBL_MAIN.KYC_MAIN
   where rownum = 1;

  --l_str:='lv_kyc_main_GBL.legal_name'||':='||'''TEST''';

  l_str := 'lv_kyc_main_GBL.legal_name := ''TEST''';
  dbms_output.put_line(l_str);

  execute immediate (l_str);
end;
/

Error report:
ORA-00900: invalid SQL statement
ORA-06512: at line 14
00900. 00000 -  "invalid SQL statement"
*Cause:    
*Action:
lv_kyc_main_GBL.legal_name := 'TEST'

I can't understand what is wrong in the following assignment. It just works fine if I do the assignment separately as:

lv_kyc_main_GBL.legal_name := 'TEST'
2
  • Have you tried to add the ; on your assignment? l_str := 'lv_kyc_main_GBL.legal_name := ''TEST'';'; Commented Feb 11, 2016 at 12:23
  • @JorgeCampos : I tried that just now still the same error is coming . Commented Feb 11, 2016 at 12:44

1 Answer 1

6

I am not really sure what you are trying to achieve, but to run a PLSQL block (ie not an SQL statement) in execute immediate, you need to wrap it in begin ... end;

Also, you cannot assign to a variable outside the execute immediate block from within it, so in your example, attempting to assign directly to lv_kyc_main_GBL.legal_name will not work. You need to use bind variables for this. I think something like the following will work (I don't have Oracle running here right now):

declare
  lv_kyc_main_GBL KYC_GBL_MAIN.KYC_MAIN%rowtype;
  l_str varchar2(400);

  a number(10);
begin
 select * into lv_kyc_main_GBL
 from KYC_GBL_MAIN.KYC_MAIN where rownum=1;

 l_str := 'begin :b1 := ''TEST''; end';
 dbms_output.put_line(l_str);

 execute immediate (l_str) using out lv_kyc_main_GBL.legal_name;
end;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a ton. This works. If I am using two bind variables b1 and b2 there how should the execute immediate statement look like ?
Just separate the parameters with comma using out lv_kyc_main_GBL.legal_name, anotherVar;
@JorgeCampos: The main objective behind using an assignment like that is I am having a varchar field_name whose value denote the field name of lv_kyc_main_GBL row variable and another varchar field_value which denote the value of lv_kyc_main_GBL.field_name. I need to run something like lv_kyc_main_GBL.field_name=field_value . Where field_name and field_value are both variables. How to solve such a case ?
l_str := 'begin :b1 := :b2; end'; and pass both variables as mentioned im my earlier comment.
@JorgeCampos Just saw this comment now, this should work. Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.