pl/sql case when then

--查詢出150號員工的工資,若其工資大於或等 於10000,則打印'打印salary>=10000';
--若其工資大於5000且小於10000,則打印出“5000<salary<10000”否則打印出“salary<5000”
可以這樣寫
declare
v_sal employees.salary% type;
v_tem VARCHAR2(30);
begin 
select salary into  v_sal from employees where employee_id=100;
 v_tem:=
case
when  v_sal>=10000 then'salary>=10000'
when  v_sal>5000 and v_sal <10000 then  '5000<salary<10000'
else 'salary<5000'
end ;
dbms_output.put_line(v_tem);
end;
這樣寫也能輸出,暫時無法搞明白其中原由,但可見其規律
set serveroutput on;
declare
v_sal employees.salary% type;
v_tem VARCHAR2(30);
begin 
select salary into  v_sal from employees where employee_id=100;
case
when  v_sal>=10000 then v_tem:='salary>=10000';
when  v_sal>5000 and v_sal <10000 then  v_tem:='5000<salary<10000';
else v_tem:='salary<5000';
end case;
dbms_output.put_line(v_tem);
end;




declare
v_jobid employees.job_id % TYPE;
v_temp VARCHAR2(30);
begin 
select job_id into v_jobid from employees where employee_id=122;
v_temp:=
case v_jobid
when 'IT_PROG' then 'a'
WHEN 'AC_MGT' then 'b'
when 'AC_ACCOUNT' then 'c'
ELSE 'D'
end ;
dbms_output.put_line(v_temp);
end;
set serveroutput on;
方法二:
declare
v_jobid employees.job_id % TYPE;
v_temp VARCHAR2(30);
begin 
select job_id into v_jobid from employees where employee_id=122;


case v_jobid
when 'IT_PROG' then v_temp:= 'a';
WHEN 'AC_MGT' then v_temp:= 'b';
when 'AC_ACCOUNT' then v_temp:= 'c';
ELSE v_temp:= 'D';
end case;
dbms_output.put_line(v_temp);
end;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章