Pl-sql Programming Basics (2)

Source: Internet
Author: User
Tags case statement

First, the control statement

1. If statement

Grammar:

IF < Boolean expressions > Then

PL/SQL and statements

END IF;

Example:

DECLARE  Number: = & n;    BEGIN  IF > 0  Then         dbms_output.put_line (' positive number ')  ; END IF ; END;

Determine if it is a positive integer!

2. If...else ...

Grammar

IF < Boolean expressions > Then

PL/SQL and statements

ELSE

Other statements

END IF;

Example:

DECLAREV_count Number:= &N;BEGIN    IFV_count> 0  ThenDbms_output.put_line ('Positive'); ELSEDbms_output.put_line ('Negative number'); END IF;END;

Determine if it is a positive or negative number!

3, multi-if...else ... Structure

Grammar:

IF < Boolean expressions > Then

PL/SQL and statements

Elsif < Other Boolean expressions > Then

PL/SQL and statements

Elsif < Other Boolean expressions > Then

PL/SQL and statements

ELSE

PL/SQL and statements

END IF;

Example:

DECLAREV_count Number:= &N;BEGIN  IFV_count> 0  ThenDbms_output.put_line ('Positive'); elsif V_count< 0  ThenDbms_output.put_line ('Negative number'); ELSEDbms_output.put_line ('0'); END IF;END;

4. Case statement Syntax:

Format one:

Case conditional expression

When conditional expression result 1 Then

Statement Segment 1

When conditional expression Result 2 Then

Statement segment

......

When conditional expression Results n Then

Statement Segment N

[ELSE statement Segment]

END case;

Example: Show week based on input 1-7

DECLAREV_week Number:= &S;BEGIN   CaseV_week when 1  ThenDbms_output. Put_Line ('Monday');  when 2  ThenDbms_output. Put_Line ('Tuesday');  when 3  ThenDbms_output. Put_Line ('Wednesday');  when 4  ThenDbms_output. Put_Line ('Thursday');  when 5  ThenDbms_output. Put_Line ('Friday');  when 6  ThenDbms_output. Put_Line ('Saturday');  when 7  ThenDbms_output. Put_Line ('Sunday'); ELSEDbms_output. Put_Line ('Input Error'); END  Case;END;

Format two:

Case

When conditional expression 1 then

Statement Segment 1

When conditional expression 2 Then

Statement segment

......

When conditional expression n Then

Statement Segment N

[ELSE statement Segment]

END case;

Example: Based on fractional output a,b,c,d,e

DECLAREV_score Number:= &N;BEGIN   Case     whenV_score>=  -  ThenDbms_output. Put_Line ('A');  whenV_score>=  the  ThenDbms_output. Put_Line ('B');  whenV_score>=  -  ThenDbms_output. Put_Line ('C');  whenV_score>=  -  ThenDbms_output. Put_Line ('D'); ELSEDbms_output. Put_Line ('E'); END  Case;END;

Pl-sql Programming Basics (2)

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.