GVKun编程网logo

Oracle CASE语句?(oraclecase语句)

6

最近很多小伙伴都在问OracleCASE语句?和oraclecase语句这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展delphi–编译器如何处理case语句?、mysql语句转

最近很多小伙伴都在问Oracle CASE语句?oraclecase语句这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展delphi – 编译器如何处理case语句?、mysql语句转为oracle语句、Oracle Decode()函数和CASE语句的比较、oracle if else语句使用介绍等相关知识,下面开始了哦!

本文目录一览:

Oracle CASE语句?(oraclecase语句)

Oracle CASE语句?(oraclecase语句)

我有一个存储过程。我在这里传递一个布尔值,例如IS_ELIGIBLE。现在,我希望能够编写如下查询:

SELECT col1,       CASE           WHEN IS_ELIGIBLE THEN col2 * 100       ELSE          col2 * 50       END         INTO OUT_COL1, OUT_SCOREFROM TABLE_NAME

问题是由于IS_ELIGIBLE不是TABLE_NAME中的列之一,查询出错了。我可以使用if..else即写相同的查询。

IF IS_ELIGIBLE   SELECT col1, col2 * 100ELSE   SELECT col1, col2 * 50END

但是我将重复两次select语句。我知道我可以创建具有该select语句的函数,这样我就不必重复两次。但是我只是很好奇是否可以在不执行if..else或创建新函数的情况下完成?谢谢。

答案1

小编典典

问题不在于IS_ELIGIBLE不是表中的列,而是它是布尔值并且SQL无法处理布尔值(我知道,不要问)。因此,您需要引入另一个类似这样的变量:

IS_ELIGIBLE_NUM NUMBER := CASE WHEN IS_ELIGIBLE THEN 1 ELSE 0 END;..SELECT col1,   CASE       WHEN IS_ELIGIBLE_NUM = 1 THEN col2 * 100   ELSE      col2 * 50   END     INTO OUT_COL1, OUT_SCOREFROM TABLE_NAME

delphi – 编译器如何处理case语句?

delphi – 编译器如何处理case语句?

在 the documentation案例陈述中,它说:

Each value represented by a caseList must be unique in the case
statement;

如图所示,

case I of
  1..5: Caption := 'Low';
  6..9: Caption := 'High';
  0,10..99: Caption := 'Out of range';
else
  Caption := ;
end

相当于嵌套条件:

if I in [1..5] then
  Caption := 'Low';
else if I in [6..10] then
  Caption := 'High';
else if (I = 0) or (I in [10..99]) then
  Caption := 'Out of range'
else
  Caption := ;

所以第一个引用表明它被处理成一个集合(阅读评论here至少有一个人与我在一起).

现在我知道这部分了

where selectorExpression is any expression of an ordinal type smaller
than 32 bits

与集合的属性相矛盾,因为它在集合中提到了her:

The base type can have no more than 256 possible values,and their
ordinalities must fall between 0 and 255

真正困扰我的是为什么在caseList中拥有唯一值是必要的.如果它等同于if语句,那么第二个值将不会被测试,因为编译器已经找到了先前的匹配?

解决方法

该文档采用特定的case语句,该语句等同于特定的if语句.

通常,任何case语句都可以使用相同的方法重写为if语句.但是,反之则不然.

该文档使用等效的if语句来解释case语句的逻辑行为(或语义).它不是编译器内部工作的表示.

How does the compiler handle case statement?

首先说明这个问题有两个方面.

>从语义上讲,编译器必须处理文档中指出的case语句.这包括:

>确保可以在编译时评估每个caseList条目的值.
>确保caseList条目是唯一的.
>无论caseList条目匹配,都会调用相应的caseList语句.
>如果没有caseList条目匹配,则调用else语句.

>但是,如果优化的字节/机器代码在逻辑上等效,则编译器有权优化其认为合适的实现.

> Johan’s answer描述了常见的优化:跳转列表和重新排序.
>鉴于严格的语义,这些更容易应用.

What is really bugging me is that why it is a necessity to have a unique values in the caseList.

消除歧义需要唯一性.如果多个匹配,应该使用哪个caseList语句?

>它可以调用第一个匹配的caseList语句并忽略其余的. (sql Server CASE语句的行为与此类似.)另请参阅下面的[1].
>它可以打电话给所有人. (如果我没记错的话,MANTIS编程语言将其语义用于case语句的版本.)
>它可以报告错误,要求程序员消除caseList的歧义. (简单地说,这就是德尔福规范所要求的.许多其他语言使用相同的方法.对它进行狡辩是非生产性的,特别是因为这种选择不太可能是一个障碍.)

If it is equivalent to the if statement the second value would be just not tested because the compiler already found a prior match.

[1]我想指出,这会使代码更难以阅读.使用魔术文字时,此行为是“正常”,但使用const标识符时,这会变得很危险.如果2个不同的consts具有相同的值,则不会立即显示caseList也匹配的后一个caseList语句将不会被调用.由于简单的caseList重新排序,case语句也会受到行为改变的影响.

const
  NEW_CUSTOMER = 0;
  EDIT_CUSTOMER = 1;
  ...
  CANCEL_OPERATION = 0;

case UserAction of
  NEW_CUSTOMER : ...;
  EDIT_CUSTOMER : ...;
  ...
  CANCEL_OPERATION : ...; { Compiler error is very helpful. }
end;

ConTradicts with the properties of sets

没有矛盾.每个caseList值必须唯一的事实不以任何方式暗示它必须“像集合一样处理”.那是你不正确的假设.做出相同假设的其他人也同样不正确.

如何检查唯一性约束取决于编译器.我们只能推测.但我猜最有效的方法是维护一个有序的范围列表.通过每个caseList的值和范围,找到它在上面列表中的位置.如果它重叠,则报告错误,否则将其添加到列表中.

mysql语句转为oracle语句

mysql语句转为oracle语句

mysql建表语句转为oracle建表语句


单引号''变双引号"

comment内容去掉

varchar变varchar2

double/int之类的数字变number

PRIMARY KEY  ("kc_no")语句变 CONSTRAINT "kc_no" PRIMARY KEY  ("kc_no")


select jihua_no from zhou.px_kecheng_jihua 

where status<>-1 

and input_date between "0001-01-01" and "9999-12-31" 

order by input_date desc

mysql语句转为oracle语句日期不再是字符串需要to_date

select jihua_no from px_kecheng_jihua 

where status<>-1 

and input_date between to_date(''0001-01-01'',''yyyy-mm-dd'') and to_date(''9999-12-31'',''yyyy-mm-dd'')

order by input_date desc



用java设置日期

            pStmt.setDate(13,new java.sql.Date(Calendar.getInstance().getTimeInMillis()));

            pStmt.setDate(14,new java.sql.Date(order.getPxDate().getTime()));


驱动程序名称

MySQL-AB JDBC Driver

Oracle JDBC driver

可以通过

conn.getMetaData().getDriverName().contains("MySQL")判断是何数据库

例如:

 if(conn.getMetaData().getDriverName().contains("MySQL")==true)

                sqlStr="select jihua_no from px_kecheng_jihua where status<>-1 and "+column+" like ? and input_date between ? and ? order by input_date desc";

            if(conn.getMetaData().getDriverName().contains("Oracle")==true)

                sqlStr="select jihua_no from px_kecheng_jihua where status<>-1 and "+column+" like ? and input_date between to_date(?,''yyyy-mm-dd'') and to_date(?,''yyyy-mm-dd'') order by input_date desc";

            


Oracle Decode()函数和CASE语句的比较

Oracle Decode()函数和CASE语句的比较

Oracle Decode()函数和CASE语句都是我们经常用到的,那么它们的区别在哪里呢?下面就为您详细介绍 Oracle Decode()函数和CASE语句的区别,供您参考。 首先,举2个简单的例子,简单对比一下这2者的区别。 1.CASE语句: 以下是代码片段: SELECTCASESIGN(5-5) W

      oracle decode()函数和case语句都是我们经常用到的,那么它们的区别在哪里呢?下面就为您详细介绍       

     Oracle Decode()函数和CASE语句的区别,供您参考。

  首先,举2个简单的例子,简单对比一下这2者的区别。

  1.CASE语句:

  以下是代码片段:

  SELECT CASE SIGN(5 - 5)

WHEN 1 THEN ''Is Positive''

WHEN -1 THEN ''Is Negative''

ELSE ''Is Zero'' END

FROM DUAL;

  后台实现:

  以下是代码片段:

  if (SIGN(5 – 5) = 1) {

''Is Positive'';

} else if (SIGN(5 – 5) = 2 ) {

''Is Negative'';

}else {

‘Is Zero’

}

  2. Decode函数:

  以下是代码片段:

  SELECT DECODE(SIGN(5 – 5), 1,

''Is Positive'', -1, ''Is Negative'', ‘Is Zero’)

FROMDUAL

  后台实现:

  以下是代码片段:

  switch ( SIGN(5 – 5) )

{

case 1 : ''Is Positive''; break;

case 2 : ''Is Negative''; break;

default : ‘Is Zero’

}

  在上面的例子中,2者似乎都可以实现。但是,在碰到非凡的问题时Decode()要实现起来就相当复杂了。

  例如:

  以下是代码片段:

  SELECT CASE X-FIELD

WHEN X-FIELD 
WHEN X-FIELD 
WHEN X-FIELD 
ELSE ‘UNBEKNOWN’END

FROM DUAL

  因此,个人认为,,CASE语句在处理类似问题就显得非常灵活。当只是需要匹配少量数值时,用Decode更为简洁。

oracle if else语句使用介绍

oracle if else语句使用介绍

Oracle if else 语句的写法及应用介绍,详细可参考本文

接收contract_no和item_no值,在inventory表中查找,如果产品:
已发货,在arrival_date中赋值为今天后的7天
已订货,在arrival_date中赋值为今天后的一个月
既无订货又无发货,则在arrival_date中赋值为今天后的两个月,
并在order表中增加一条新的订单记录。

product_status的列值为''shipped''和''ordered''
inventory:
product_idnumber(6)
product_descriptionchar(30)
product_statuschar(20)
std_shipping_qtynumber(3)
contract_item:
product_id number(6)
contract_nonumber(12)
item_nonumber(6)
arrival_datedate
order:
order_idnumber(6)
product_idnumber(6)
qtynumber(3)

代码:
代码如下:
declare
i_product_id inventory.product_id%type;
i_product_description inventory.product_description%type;
i_product_status inventory.product_status%type;
i_std_shipping_qty inventory.std_shipping_qty%type;
begin
//sql语句,将查询出来的值放到定义的变量中
select product_id, product_description, product_status, std_shipping_qty
into i_product_id, i_product_description, i_product_status, i_std_shipping_qty
from inventory where product_id=(
select product_id from contract_item where contract_no=&&contract_no and item_no=&&item_no
);
if i_product_status=''shipped'' then
update contract_item set arrival_date=sysdate+7 contract_no=&&contract_no and item_no=&&item_no;
//这里的elseif 是连着写的
elseif i_product_status=''ordered''then
updatecontract_item
setarrival_date=add_months(sysdate,1)//加一个月
whereitem_no=&&itemnoandcontract_no=&&contractno;
else
updatecontract_item
setarrival_date=add_months(sysdate,2)
whereitem_no=&&itemnoandcontract_no=&&contractno;
insertintoorders
values(100,i_product_id,i_std_shipping_qty);
end if;
end if;
commit;
end;

我们今天的关于Oracle CASE语句?oraclecase语句的分享就到这里,谢谢您的阅读,如果想了解更多关于delphi – 编译器如何处理case语句?、mysql语句转为oracle语句、Oracle Decode()函数和CASE语句的比较、oracle if else语句使用介绍的相关信息,可以在本站进行搜索。

本文标签: