Home Database Mysql Tutorial oracle/plsql case条件语句的用法

oracle/plsql case条件语句的用法

Jun 07, 2016 pm 05:46 PM
Conditional statements

本文章要介绍关于oracle/plsql case条件语句的用法,它和mysql mssql都差不多,好了费话不说多了需要的学同可以看看吧。

本文章要介绍关于oracle/plsql case条件语句的用法,它和mysql mssql都差不多,好了费话不说多了需要的学同可以看看吧。

语句语法

 代码如下 复制代码

CASE  [ expression ]
  WHEN condition_1 THEN result_1
  WHEN condition_2 THEN result_2
  ...
  WHEN condition_n THEN result_n
  ELSE result
END

expression 可选的。它的价值,你比较的条件清单。 (即:condition_1,condition_2,... condition_n)

condition_1到condition_n都必须是相同的数据类型。条件评估中列出的顺序。一个条件是一旦发现是真实的,case语句将返回的结果和不评价任何进一步的条件。

result_1到result_n都必须是相同的数据类型。这是返回的值一个条件是,一旦发现是真的。

注意:

如果没有条件为真,那么case语句将返回在ELSE子句里的值。

如果省略了ELSE子句和任何条件发现是真的,那么case语句将返回NULL。

最多可以有255在case语句比较。时,每个...条款被认为是2比较。


Applies To:

Oracle 9i, Oracle 10g, Oracle 11g


实例
你可以使用case语句在SQL语句如下:(包括表达式子句)

 代码如下 复制代码

table_name,
CASE owner
  WHEN 'SYS' THEN 'The owner is SYS'
  WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
  ELSE 'The owner is another value'
END
from all_tables;


或者你可以写SQL语句,使用这样的情况下声明:(省略了表达式子句)

 代码如下 复制代码

select table_name,
CASE
  WHEN owner='SYS' THEN 'The owner is SYS'
  WHEN owner='SYSTEM' THEN 'The owner is SYSTEM'
  ELSE 'The owner is another value'
END
from all_tables;

上述两个案例语句以下的IF - THEN- ELSE语句是等价的:

 代码如下 复制代码

IF owner = 'SYS' THEN
     result := 'The owner is SYS';

ELSIF owner = 'SYSTEM' THEN
    result := 'The owner is SYSTEM'';

ELSE
    result := 'The owner is another value';

END IF;

case语句会比较每一位业主的价值,一个接一个。

需要注意的一点是,在case语句的else子句是可选的的。你可以省略。让我们看看上面的SQL语句与ELSE子句省略。

您的SQL语句如下所示:

 代码如下 复制代码

select table_name,
CASE owner
  WHEN 'SYS' THEN 'The owner is SYS'
  WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
END
from all_tables;

实例
下面就是一个例子,演示了如何使用case语句来比较不同条件下:

 

 代码如下 复制代码
select
CASE
  WHEN a   WHEN d END
from suppliers;
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
C program to print 'even' or 'odd' without using conditional statements C program to print 'even' or 'odd' without using conditional statements Sep 15, 2023 pm 09:21 PM

In this section, we will see how to check whether a number is odd or even without using any conditional statements like <, <=, !=, >, >=, ==. We can easily check if the number is odd or even by using conditional statement. We can divide the number by 2 and check if the remainder is 0. If 0, it is an even number. Otherwise, we can AND the number with 1. If the answer is 0, it is an even number, otherwise it is an odd number. Conditional statements cannot be used here. We'll see two different ways to check whether an odd or even number is present. Method 1 Here we will create an array of strings. The index 0 position will hold "even" and the index 1 position will hold "odd". We can divide numbers

5 essential Python entry example codes 5 essential Python entry example codes Jan 13, 2024 am 08:39 AM

Getting Started with Python Code: 5 Essential Examples for Learning Python is a simple and easy-to-learn high-level programming language that is widely used in data analysis, machine learning, web crawlers and other fields. For beginners, it is important to master some basic Python code. This article will introduce 5 simple example codes to help beginners quickly get started with Python programming. Print Hello,World!print("Hello,World!") This is Python

Program to print numbers from 1 to 100 without using loops Program to print numbers from 1 to 100 without using loops Sep 07, 2023 pm 05:49 PM

Here we will see how to write a C program that can print numbers from 1 to 100 without using any kind of loop. This problem can be solved using recursion. We will create a function that will be called recursively. We know that recursive functions basically have two parts. Operations such as base cases and recursive calls. In this function, the base case is that parameter n is greater than 1. The function will be called recursively until 1 is reached. Now finally it will print the value of n. This way the entire system generates numbers. Sample code #include<stdio.h>voidgenerate_numbers(intn){if(n>1){generate_nu

Conditional statement usage and examples in C++ Conditional statement usage and examples in C++ Aug 22, 2023 am 08:25 AM

As a high-level programming language, C++ has a variety of flow control statements to implement the decision-making structure and loop structure of the program. Among them, the conditional statement is one of the most commonly used statements in C++ programming. It determines the execution path of the program by judging whether the condition is met. This article will introduce the usage and examples of conditional statements in C++ in detail to help readers better understand and apply this syntax. 1. Basic syntax of conditional statements Conditional statements in C++ mainly include three types: if statement, ifelse statement and switch statement. their basic language

What are the three forms of conditional statements? What are the three forms of conditional statements? Jan 11, 2024 pm 01:37 PM

Three forms of conditional statements: 1. if statement: the syntax is "if (condition) {execute statement}", if the condition is true, the statement is executed; 2. if-else statement: the syntax is "if (condition) {execute Statement 1 } else {Execute statement 2 }", if the condition is true, execute statement 1; otherwise, execute statement 2; 3, switch statement, etc.

How to use conditional statements in Python? How to use conditional statements in Python? Jun 04, 2023 pm 03:10 PM

Conditional statements in the Python language are an important programming concept that are often used to control the flow of the program and determine whether to perform different operations under different circumstances. In Python, commonly used conditional statements include if statements and if-else statements. This article will introduce how to use conditional statements in Python. 1. Basic usage of if statement The if statement is one of the most commonly used conditional statements in Python. It is used to execute a block of code under specific conditions. Its basic syntax is as follows: ifcondition

How to use conditional statements in Java to make logical judgments How to use conditional statements in Java to make logical judgments Oct 26, 2023 am 09:18 AM

How to use conditional statements in Java to make logical judgments requires specific code examples. Conditional statements are a commonly used tool in programming, which enable the program to perform different branch executions according to requirements. In Java programs, conditional statements can be used to determine the next action of the program based on the truth or falsehood of a certain condition. This article will introduce the use of conditional statements in Java and give specific code examples. In Java, there are two main forms of conditional statements: if statements and switch statements. if statement if statement is the most commonly used conditional statement

What are the common conditional statements in PHP programming? What are the common conditional statements in PHP programming? Jun 12, 2023 am 08:25 AM

PHP is an open source, general-purpose scripting language that is widely used in the field of web development. In PHP programming, conditional statements are one of the indispensable basic syntaxes, used to implement various logical judgments and flow control in the program. This article will introduce common conditional statements in PHP programming. 1. If statement The most commonly used conditional statement in PHP is the if statement. The syntax of the if statement is as follows: if (conditional expression) {//statement to be executed when the condition is true} where the conditional expression can be anything

See all articles