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!

Declare variable based on column value result

The_Cute_DBAOct 29 2020 — edited Oct 29 2020

Hello,
I have table as shown below:
SQL> select * from mydata;

MAIN COMP
---------- ------------------
sales mycorporation.com
training
delivery
marketing mycorporation.com

I would like to define variables depending on the result of my select statement. The idea is something like this:
v_result should print 'main.comp' if comp is not null
v_result should print 'main.none' if comp is null

The idea I have is something like below, but I know it's is not right. Can you help me figure out the right way?
Thanks a lot!

declare
v_main varchar2(50);
v_comp varchar2(50);
BEGIN
select main into v_main from mydata;
select comp into v_comp from mydata;

case when (v_comp is not null) then
v_result := v_main||'.'||v_comp;
else
v_result := v_main||'.none';
end;

dbms_output.Put_line(v_result);
end;

This post has been answered by Frank Kulash on Oct 29 2020
Jump to Answer
Comments
Post Details
Added on Oct 29 2020
7 comments
548 views