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.
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;
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; /
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; /
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.
IF sayi1<sayi2 THEN
ELSEIF sayi2<sayi3 THEN
ELSEIF sayi3<sayi2 THEN
DBMS_OUTPUT.PUT_LINE(‘Sonuç = ‘||sonuc);
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.
605 views last month, 2 views today