查看: 3595|回复: 8

case语句,求助高手

[复制链接]
论坛徽章:
38
鲜花蛋
日期:2011-10-19 18:13:09慢羊羊
日期:2015-04-09 14:37:382015年新春福章
日期:2015-03-06 11:57:312015年新春福章
日期:2015-03-04 14:51:12马上加薪
日期:2015-02-06 10:32:29马上有钱
日期:2015-02-03 10:22:062014年世界杯参赛球队: 尼日利亚
日期:2014-06-13 16:43:20奥迪
日期:2014-01-13 13:09:45福特
日期:2013-11-19 14:06:56红旗
日期:2013-11-19 11:38:42
跳转到指定楼层
1#
发表于 2006-8-24 10:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
几个when分支执行相同操作,怎么合并到一块
比如:
case  selector
when a then A;
when b then A;
when c then B;
when d then B;
when e then C
end case;
论坛徽章:
57
马上加薪
日期:2014-02-19 11:55:142011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:41:01ITPUB9周年纪念徽章
日期:2010-10-08 09:28:522010系统架构师大会纪念
日期:2010-09-03 16:39:572010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010新春纪念徽章
日期:2010-03-01 11:21:02
2#
发表于 2006-8-24 10:15 | 只看该作者
case selector
when a or b then A;
when c or d then B;
when e then C
end case;

使用道具 举报

回复
论坛徽章:
38
鲜花蛋
日期:2011-10-19 18:13:09慢羊羊
日期:2015-04-09 14:37:382015年新春福章
日期:2015-03-06 11:57:312015年新春福章
日期:2015-03-04 14:51:12马上加薪
日期:2015-02-06 10:32:29马上有钱
日期:2015-02-03 10:22:062014年世界杯参赛球队: 尼日利亚
日期:2014-06-13 16:43:20奥迪
日期:2014-01-13 13:09:45福特
日期:2013-11-19 14:06:56红旗
日期:2013-11-19 11:38:42
3#
 楼主| 发表于 2006-8-24 10:19 | 只看该作者
最初由 zhouwf0726 发布
[B]case selector
when a or b then A;
when c or d then B;
when e then C
end case; [/B]



以前试过,不好用

PLS-00382 expression is of wrong type

使用道具 举报

回复
论坛徽章:
57
马上加薪
日期:2014-02-19 11:55:142011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:41:01ITPUB9周年纪念徽章
日期:2010-10-08 09:28:522010系统架构师大会纪念
日期:2010-09-03 16:39:572010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010新春纪念徽章
日期:2010-03-01 11:21:02
4#
发表于 2006-8-24 10:43 | 只看该作者
set serverout on

create table test (id varchar2(10));
insert into test values('1');
insert into test values('2');
insert into test values('3');
insert into test values('4');
insert into test values('5');
commit;

declare
cursor cur_test is select id from test;
begin
        for row_cur in cur_test loop
                case when row_cur.id='1' or row_cur.id='2' then
                        dbms_output.put_line('A');
                when row_cur.id='3' or row_cur.id='4' then
                        dbms_output.put_line('B');
                else
                        dbms_output.put_line('C');
                end case;
        end loop;
end;
/


declare
cursor cur_test is select id from test;
begin
        for row_cur in cur_test loop
                dbms_output.put_line(
                case when row_cur.id='1' or row_cur.id='2' then
                        'A'
                when row_cur.id='3' or row_cur.id='4' then
                        'B'
                else
                        'C'
                end);
        end loop;
end;
/


declare
cursor cur_test is
        select case
        when id='1' or id='2' then 'A'
        when id='3' or id='4' then 'B'
        else 'C' end id
         from test;
begin
        for row_cur in cur_test loop
                dbms_output.put_line(row_cur.id);
        end loop;
end;
/

使用道具 举报

回复
论坛徽章:
38
鲜花蛋
日期:2011-10-19 18:13:09慢羊羊
日期:2015-04-09 14:37:382015年新春福章
日期:2015-03-06 11:57:312015年新春福章
日期:2015-03-04 14:51:12马上加薪
日期:2015-02-06 10:32:29马上有钱
日期:2015-02-03 10:22:062014年世界杯参赛球队: 尼日利亚
日期:2014-06-13 16:43:20奥迪
日期:2014-01-13 13:09:45福特
日期:2013-11-19 14:06:56红旗
日期:2013-11-19 11:38:42
5#
 楼主| 发表于 2006-8-24 10:53 | 只看该作者
四楼,多谢~~~~~~~~

使用道具 举报

回复
论坛徽章:
2
生肖徽章2007版:猪
日期:2008-02-25 09:49:28奥运会纪念徽章:羽毛球
日期:2008-05-24 11:31:29
6#
发表于 2006-8-24 10:54 | 只看该作者

Re: case语句,求助高手

最初由 wildcatqfj 发布
[B]几个when分支执行相同操作,怎么合并到一块
比如:
case  selector
when a then A;
when b then A;
when c then B;
when d then B;
when e then C
end case; [/B]


使用DECODE函数也许会更好一些!

使用道具 举报

回复
论坛徽章:
38
鲜花蛋
日期:2011-10-19 18:13:09慢羊羊
日期:2015-04-09 14:37:382015年新春福章
日期:2015-03-06 11:57:312015年新春福章
日期:2015-03-04 14:51:12马上加薪
日期:2015-02-06 10:32:29马上有钱
日期:2015-02-03 10:22:062014年世界杯参赛球队: 尼日利亚
日期:2014-06-13 16:43:20奥迪
日期:2014-01-13 13:09:45福特
日期:2013-11-19 14:06:56红旗
日期:2013-11-19 11:38:42
7#
 楼主| 发表于 2006-8-24 15:15 | 只看该作者

Re: Re: case语句,求助高手

最初由 淡若煙飛 发布
[B]

使用DECODE函数也许会更好一些! [/B]



不行啊
Compilation errors for FUNCTION MSS.TEST

Error: PLS-00204: 函数或伪列 'DECODE' 只能在 SQL 语句中使用


PLS-00204 function or pseudo-column 'string' may be used inside a SQL statement only

Cause: A pseudocolumn or proscribed function was used in a procedural statement. The SQL pseudocolumns (CURRVAL, LEVEL, NEXTVAL, ROWID, ROWNUM) can be used only in SQL statements. Likewise, certain functions such as DECODE, DUMP, and VSIZE and the SQL group functions (AVG, MIN, MAX, COUNT, SUM, STDDEV, VARIANCE) can be used only in SQL statements.

Action: Remove the pseudocolumn reference or function call from the procedural statement. Or, replace the procedural statement with a SELECT INTO statement; for example, replace:

bonus := DECODE(rating, 1, 5000, 2, 2500, ...);


with the following statement:

SELECT DECODE(rating, 1, 5000, 2, 2500, ...) INTO bonus FROM dual;

使用道具 举报

回复
论坛徽章:
0
8#
发表于 2006-8-25 11:28 | 只看该作者
最初由 wildcatqfj 发布
[B]四楼,多谢~~~~~~~~ [/B]


觉得很好

使用道具 举报

回复
论坛徽章:
2
生肖徽章2007版:猪
日期:2008-02-25 09:49:28奥运会纪念徽章:羽毛球
日期:2008-05-24 11:31:29
9#
发表于 2006-8-25 11:40 | 只看该作者

Re: Re: Re: case语句,求助高手

最初由 wildcatqfj 发布
[B]


不行啊
Compilation errors for FUNCTION MSS.TEST

Error: PLS-00204: 函数或伪列 'DECODE' 只能在 SQL 语句中使用


PLS-00204 function or pseudo-column 'string' may be used inside a SQL statement only

Cause: A pseudocolumn or proscribed function was used in a procedural statement. The SQL pseudocolumns (CURRVAL, LEVEL, NEXTVAL, ROWID, ROWNUM) can be used only in SQL statements. Likewise, certain functions such as DECODE, DUMP, and VSIZE and the SQL group functions (AVG, MIN, MAX, COUNT, SUM, STDDEV, VARIANCE) can be used only in SQL statements.

Action: Remove the pseudocolumn reference or function call from the procedural statement. Or, replace the procedural statement with a SELECT INTO statement; for example, replace:

bonus := DECODE(rating, 1, 5000, 2, 2500, ...);


with the following statement:

SELECT DECODE(rating, 1, 5000, 2, 2500, ...) INTO bonus FROM dual; [/B]


SELECT DECODE(rating, 1, 5000, 2, 2500, ...) INTO bonus FROM dual;

这句是不是有些问题?
能把你的函数代码贴出来看看么?

使用道具 举报

回复

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

本版积分规则 发表回复

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