PL/SQL Programing -5 IF-ELSE Condition

Hi all in this article we continue with the flow operators in PL / SQL Programming. You can browse our previous article before moving on to this article

 

Structures like IF-ELSE in PL / SQL are similar to many programming languages. In its simplest form, TRUE results in the return of FALSE in an undesirable or different result if a condition is desired in a desired state.

 

IF-THEN YAPISI:

Basically there is the IF-THEN syntax with the condition and the block of code that will then run. IF-THEN structure is closed with END-IF block after code execution.

IF THEN condition

PL / SQL Code;

……..

…….

END IF;

 

SQL>set serveroutput on;

DECLARE

sayi1 NUMBER:=5;

sayi2 NUMBER:=2;

fark NUMBER;

BEGIN

IF sayi1>sayi2 THEN

fark:=sayi1-sayi2;

END IF;

DBMS_OUPUT.PUT_LINE('SAYI1 = '||sayi1);

DBMS_OUPUT.PUT_LINE('SAYI2 = '||sayi2);

DBMS_OUPUT.PUT_LINE('FARK = '||fark);

END;
/



IF-THEN-ELSE:

This structure contains the ELSE block in addition to the previous one. IF-THEN is the block that contains the codes that will be executed when the condition is not met. Explain through the example would be the most tutorial.

DECLARE 
sayi1 NUMBER:=10; 
sayi2 NUMBER:=5; 
sonuc NUMBER: 
BEGIN 
IF sayi1<sayi2 THEN 
sonuc:=sayi1+sayi2; 
ELSE 
sonuc:=sayi1*sayi2; 
END IF; 
DBMS_OUTPUT.PUT_LINE('Sonuç : '||sonuc); 
END; 
/

 

 

IF-THEN-ELSIF:

It is also a block structure that works in a similar manner to the previous one, but can only be used if more than one possible situation is desired instead of a single ELSE structure.

DECLARE
sayi1 NUMBER:=5;
sayi2 NUMBER:=4;
sayi3 NUMBER:=3;
sonuc NUMBER;
BEGIN
IF sayi1<sayi2 THEN
sonuc:=sayi1+sayi2;
ELSEIF sayi2<sayi3 THEN
sonuc:=sayi2+sayi3;
ELSEIF sayi3<sayi2 THEN
sonuc:=sayi3*sayi3;
ELSE sonuc:=0;
END IF;
DBMS_OUTPUT.PUT_LINE(‘Sonuç = ‘||sonuc);
END;
/


I tried to explain the examples in a very simple way, but complex queries such as IF-THEN etc. or DML, DDL operations can be done. A table can be UPDATE if any condition is met, or a different INSERT can be entered if not provided. It is possible to duplicate such examples.

Let’s leave this article here and the next article will be looking at the CASE-WHEN and nested IF-THEN structures, to discuss.


Deniz Parlak

I am a technical author on IT Tutorial. I am working on Oracle and Mysql databases, I also work on Datawarehouse and Big Data. If you need help, please contact deniz.parlak@yahoo.com.