• OCP-1Z0-051-V9.02-97题


    97. View the Exhibit and evaluate the structure and data in the CUST_STATUS table.

    You issue the following SQL statement:

    SQL> SELECT custno, NVL2(NULLIF(amt_spent, credit_limit), 0, 1000)"BONUS"     

    FROM cust_status;

    Which statement is true regarding the execution of the above query? 

    A. It produces an error because the  AMT_SPENT column contains a  null value.

    B. It displays a bonus of 1000 for all customers whose AMT_SPENT is less than CREDIT_LIMIT.

    C. It displays a bonus of 1000 for all customers whose AMT_SPENT   equals CREDIT_LIMIT, or

    AMT_SPENT is null .

    D. It produces an error because the TO_NUMBER function must be used to convert the result of the

    NULLIF function before it can be used by the NVL2 function.

    Answer: C
     
    答案解析:

    实验验证:
    scott@TEST0924> create table cust_status
      2  (custno number(2) not null,
      3  amt_spent number(10,2),
      4  credit_limit number(10,2)
      5  )
      6  /

    Table created.

    scott@TEST0924> insert into cust_status values(1,1000,1000);

    1 row created.

    scott@TEST0924> insert into cust_status values(2,2000,2500);

    1 row created.

    scott@TEST0924> insert into cust_status values(3,'',3000);

    1 row created.

    scott@TEST0924> insert into cust_status values(4,3000,2800);

    1 row created.

    scott@TEST0924> select * from cust_status;

        CUSTNO  AMT_SPENT CREDIT_LIMIT
    ---------- ---------- ------------
             1       1000         1000
             2       2000         2500
             3                    3000
             4       3000         2800


     
    scott@TEST0924> select custno,nullif(amt_spent,credit_limit) from cust_status;

        CUSTNO NULLIF(AMT_SPENT,CREDIT_LIMIT)
    ---------- ------------------------------
             1
             2                           2000
             3
             4                           3000
    从以上sql语句得知,在amt_spent,credit_limit相等,或者AMT_SPENT为空时,返回空值。

    运行题中SQL语句
    scott@TEST0924> select custno,nvl2(nullif(amt_spent,credit_limit),0,1000) "BONUS" from cust_status;

        CUSTNO      BONUS
    ---------- ----------
             1       1000
             2          0
             3       1000
             4          0
    从以上sql得知,当nullif函数返回空值时,"BONUS" 为1000,也即是说amt_spent,credit_limit相等,或者AMT_SPENT为空时,"BONUS" 为1000。选项C正好符合此意思。
     
     
     
    1、NVL2
    2、NULLIF
     
     
     

    Purpose

    NULLIF compares expr1 and expr2. If they are equal, then the function returns null.比较expr1 andexpr2,如果相等,则返回null值。

     If they are not equal, then the function returns expr1. You cannot specify the literalNULL for expr1.如果不等,则返回expr1 。不能指定expr1为空。

    If both arguments are numeric data types, then Oracle Database determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns that data type.

    If the arguments are not numeric, then they must be of the same data type, or Oracle returns an error.

    如果参数类型不是数字类型,则必须是相同数据类型,否则报错。

    The NULLIF function is logically equivalent to the following CASE expression:

    CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
     
     
     
  • 相关阅读:
    JVM参数默认值列表
    垃圾回收G1日志解析
    《深入理解JAVA虚拟机》垃圾回收时为什么会停顿
    《深入理解JAVA虚拟机》JDK的垃圾收集算法
    什么才是技术?
    Lodash使用示例(比较全)
    MSCL超级工具类(C#),开发人员必备,开发利器
    刷新SqlServer数据库中所有的视图
    Sql Server 2014/2012/2008/2005 数据库还原出现 3154错误的解决办法
    C#中执行批处理文件(.bat),执行数据库相关操作
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317236.html
Copyright © 2020-2023  润新知