ORACLE-34-IF statement, Case statement, loop Loop, while loop, for loop

Source: Internet
Author: User
Tags case statement

One, if statement

In PL/SQL, keywords such as if, then, else, elsif, endif are used to perform conditional logic

Syntax format:


if conditions 1 Then

Statement 1

elsif conditions 2 Then

Statement 2

Else

Statement 3

End If;

Example 1: Practice If structure

Solution: Note the program in:




Second, Case statement

Syntax structure:


Case variables

when value 1 Then Statement 1;

when value 2 Then Statement 2;

when value 3 Then Statement 3;

......

when value N Then Statement N;

[Else statement ]

End case;


Where [else statements] can be written without writing.

Example 2: Practice Case statements

Solution: Note the program in:




Example 3: Practice Case statements

Solution: The current time of the output is the day of the week, first look at the current system time is the days of the week:




where To_char (sysdate, ' d ') is the current system time format, only display days, Oracle because it is a foreigner developed database system, so the 1th day of the week is Sunday, the 2nd day is Monday, ..., the 7th day is Saturday, so the program is as follows:




Third, circulation

Syntax format for 1.LOOP loops :


Loop

Looping statements

End Loop;

Like what:

Counter: = 0;

Loop

Counter: = counter + 1;

Exit when counter = 5;

End Loop;

Example 4: practicing loop loops

Solution: Note the program in:




If you find no output to the console, you can add the code as follows :




Output Result:




2.WHILE loop syntax format:


while conditions Loop

Statement

End Loop

Like what:

Counter: = 0;

While counter < 6 Loop

Counter: = Counter +1;

End Loop;

Example 5: practicing while loops

Solution: Note the program in:




3.FOR loop syntax format


for variable in [revers] variable initial value: Variable Loop End value loop

Statement

End Loop;

Like what:

for i in 1..10 loop

Dbms_output.put_line (' i = ' + i);

End Loop;

Example 6: Practice for loop

Solution: Note the program in:




Example 7: Show 99 multiplication table

Solution: Note the program in:




where Dbms_out.put () is the output statement but does not wrap, if Dbms_output.put_line () is the output statement and wraps, so the 7th line prints null after the end of the inner loop, in fact it is a newline.

Example 8: Write a PL/SQL program, put Sno,sname in the Query student table into the record table variable, and print the data from the record table variable.

Solution: Note the program in:




The 6th line is the record in the T_record to the record table T_tab, that is, T_record in the ID and name, then there are t_tab.

ORACLE-34-IF statement, Case statement, loop Loop, while loop, for loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.