pl/sql字段的問題

didiaodezhu 2009-11-30 03:38:17
SQL> select * from tb11;

ONE TWO THREE
------------------ ---------- ----------
aa bb cc
dd ee ff

我想用個函數來抓表其中幾個字段,具體是那幾個要看條件,類似

select fun(date) from
(select * from tb11);


如果是8點就顯示one two
9點就顯示two three
實際的情況字段更多 麻煩大家教下fun要怎么寫呢
本人是新人 請大家多多指教
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2009-11-30
  • 打赏
  • 举报
回复
条件很多,则需要在程序中动态拼接SQL语句
crazylaa 2009-11-30
  • 打赏
  • 举报
回复
create or replace function sel_col_by_date (
i_date in timestamp
) return sys_refcursor
as
x_sql varchar2(200);
o_cur sys_refcursor;
begin
--如果是当前时间,可以去掉i_date参数,if里面用 to_char(sysdate,'HH24')
if to_char(i_date,'HH24')='08' then
x_sql :='select one,two from tb11';
elsif to_char(i_date,'HH24')='09' then
x_sql :='select two,three from tb11';
elsif to_char(i_date,'HH24')='09' then --其他的再加elsif
x_sql :='select three,four from tb11';
else
x_sql :='select four,five from tb11';
end if;
open o_cur for x_sql;
return o_cur;
end sel_col_by_date;
/

--使用:select sel_col_by_date(sysdate) from dual;就可以了。
didiaodezhu 2009-11-30
  • 打赏
  • 举报
回复
date是當前時間
實際的字段不只有3個 是 8,9,10....17
這些字段都已經存在了
想通過一個函數篩選出其中3個字段
select fun(date) from
(select 8,9,10....17 from tb11);
根據時間顯示當前的小時已經前面兩個小時
比如 現在是 11點 就顯示 9,10,11這3個字段
15點就顯示 13,14,15這3個字段

小灰狼W 2009-11-30
  • 打赏
  • 举报
回复
是当前时间?
select decode(to_char(sysdate,'hh24'),'08',one,'09', two),
decode(to_char(sysdate,'hh24'),'08',two,'09', three)
from tb11
小灰狼W 2009-11-30
  • 打赏
  • 举报
回复
fun(date)里的date是什么?

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧