Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Select a column into a variable in dynamic SQL

Dee102Jul 7 2021 — edited Jul 7 2021

I tried both of the followings and both got error. What is the correct syntax? Thanks.
----------------------------------------------
First and the error is "[Error] Execution (1: 1): ORA-00936: missing expression ORA-06512: at line 4".
declare HQList1 varchar2(1000);
HQ varchar2(10) := 'XYZ';
begin
EXECUTE IMMEDIATE '
select HQList into ' || HQList1 || '
from HQLIST
where ID = ''' || HQ || '''';
dbms_output.put_line(HQList1);
end;
----------------------------------------------
Second and the error is "[Error] Execution (1: 1): ORA-00905: missing keyword ORA-06512: at line 4".
declare HQList1 varchar2(1000);
HQ varchar2(10) := 'XYZ';
begin
EXECUTE IMMEDIATE '
select HQList into HQList1
from HQLIST
where ID = ''' || HQ || '''';
dbms_output.put_line(HQList1);
end;
----------------------------------------------

This post has been answered by Frank Kulash on Jul 7 2021
Jump to Answer
Comments
Post Details
Added on Jul 7 2021
4 comments
5,403 views