12
返回列表 发新帖
楼主: alfah

过程中的case

[复制链接]
论坛徽章:
0
11#
发表于 2003-7-22 08:57 | 只看该作者
版本问题???
SQL> declare v number;
  2  begin
  3  select sum(case when age=1 then 1 else 3 end) into v from table_main;
  4  dbms_output.put_line(v);
  5  end;
  6  /
select sum(case when age=1 then 1 else 3 end) into v from table_main;
           *
ERROR at line 3:
ORA-06550: line 3, column 12:
PLS-00103: Encountered the symbol "CASE" when expecting one of the following:
( - + all mod null <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current distinct max min prior sql stddev sum unique
variance cast
<a string literal with character set specification>
<a number> <a single-quoted SQL string>

使用道具 举报

回复
论坛徽章:
3
会员2006贡献徽章
日期:2006-04-17 13:46:34ITPUB元老
日期:2006-04-20 11:07:35授权会员
日期:2006-07-18 13:28:23
12#
发表于 2003-7-22 08:58 | 只看该作者
9i   环境下的测试结果:

SQL> create table table_main (age float);

表已创建。

SQL> insert into table_main values(2);

已创建 1 行。

SQL>  select
  2   sum(case when age between 10 and 19 then 1 else 0 end) a1,
  3   sum(case when age between 20 and 29 then 1 else 0 end) a2,
  4   sum(case when age between 30 and 39 then 1 else 0 end) a3,
  5   sum(case when age between 40 and 49 then 1 else 0 end) a4,
  6   sum(case when age between 50 and 59 then 1 else 0 end) a5,
  7   sum(case when age between 60 and 69 then 1 else 0 end) a6,
  8   sum(case when age between 70 and 79 then 1 else 0 end) a7,
  9   sum(case when age between 80 and 89 then 1 else 0 end) a8
10   from table_main;

        A1         A2         A3         A4         A5         A6         A7
---------- ---------- ---------- ---------- ---------- ---------- ----------
        A8
----------
         0          0          0          0          0          0          0
         0

使用道具 举报

回复
论坛徽章:
0
13#
发表于 2003-7-22 09:19 | 只看该作者
呵呵,看来8.01要进故纸堆了......

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
14#
发表于 2003-7-22 09:20 | 只看该作者
以前说过多次,这是由于9i 以前的PL/SQL编译器还不支持这种语法,在 8i 中你可以使用动态SQL来实现

execute immediate 'select (case when 1>0 then 1 else 0 end) from dual' into n;

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
15#
发表于 2003-7-22 09:26 | 只看该作者
另外 8i 中的 case 不支持
case <expression> when <value1> then <result1> ...
这样的语法,在 9i 中才支持

使用道具 举报

回复
论坛徽章:
5
2014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:112015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31秀才
日期:2016-03-24 09:10:24
16#
 楼主| 发表于 2003-7-26 09:42 | 只看该作者
execute immediate 用的时间太长,还有什么好办法?

使用道具 举报

回复
论坛徽章:
4
授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34慢羊羊
日期:2015-03-04 14:19:442015年新春福章
日期:2015-03-06 11:57:31
17#
发表于 2003-7-28 00:20 | 只看该作者
in 8i :

sql support case, but pl/sql don't

使用道具 举报

回复
论坛徽章:
5
2014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:112015年新春福章
日期:2015-03-04 14:19:112015年新春福章
日期:2015-03-06 11:57:31秀才
日期:2016-03-24 09:10:24
18#
 楼主| 发表于 2003-7-31 21:10 | 只看该作者
而且没办法用游标,大家还有什么好主意?

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
19#
发表于 2003-8-1 09:17 | 只看该作者
最初由 alfah 发布
[B]而且没办法用游标,大家还有什么好主意? [/B]

可以使用动态游标啊

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表