technical skills grow

Responsive Ads Here

Thursday, November 25, 2021

PL/pgSQL IF and CASE Statement

We will learn about the case that executes statements based on a particulate condition.


Code

 -- select empcode,salary   from employee where empcode=9999;
do 
$$
declare emp_salary employee.salary%type;
result_value varchar;
begin
select salary into emp_salary from employee where empcode=9839;
if found then
case  
    when  emp_salary >1000 and emp_salary <= 2000 
then   result_value='very Low';
    when emp_salary >2000 and emp_salary <= 4000 
then   result_value='Low';
    when emp_salary >4000 and emp_salary <= 6000 
then   result_value='middle';
when emp_salary >6000 and emp_salary <= 10000 
    then  result_value='High';
    else   result_value='not found';
    end case;
   raise notice '%',result_value;
   end if;
end
$$

Task 2 :


Code 

do
$$
declare emp_details employee%rowtype;
emp_id employee.empcode%type=999;
begin
select * from employee into emp_details
where empcode = emp_id;
if not found then 
raise notice 'empcode % is not found',emp_id ;
else 
raise notice 'empcode % is and emp_name % & salary %',
emp_details.empcode,emp_details.empfname,
emp_details.salary;
end if;
end
$$



Code
do
$$
declare empdetails employee%rowtype;
job_value    employee.job%type='SALESMAN';
begin
select * from employee into empdetails
where job=job_value;
if not found then
raise notice 'Empcode % is not exist',job_value;
else
CASE
empdetails.job
when 'IT' then raise notice 'empcode % and empname %',empdetails.empcode,empdetails.empfname;
    when 'TECHNICAL LEAD' then raise notice 'empcode % and empname %',empdetails.empcode,empdetails.empfname; 
when 'SALESMAN' then raise notice 'empcode % and empname %',empdetails.empcode,empdetails.empfname; 
when 'ANALYST' then raise notice 'empcode % and empname %',empdetails.empcode,empdetails.empfname; 
else 
raise notice 'empcode % and empname %',empdetails.empcode,empdetails.empfname; 
end case;
end if;
end
$$






No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts