求救,请大侠指教。关于动态决定一个检索条件

zhangbat 2004-03-22 12:02:47
存储过程中的一个查询,用到这样一个条件:
通过一个参数判断日期类型,如果是按月,
那么检索条件中就直接指定月份
b.yearmonth = strYearMonth

如果按年就指定一个范围
b.YearMonth >= stryearFirstMonth and b.YearMonth<=stryearMonth

上面这两个都比较easy,
可是如果是安季度那就惨了,要用 in(……),就是说括弧里面的内容很难确定的

所以我想问一下,存储过程中可以动态的决定检索条件么?
要是能,怎么做(最好不要用动态sql),
如果不能,上面的问题怎么解决。
...全文
40 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ATCG 2004-03-22
  • 打赏
  • 举报
回复
当然可以了,用游标变量就可以了
zhangbat 2004-03-22
  • 打赏
  • 举报
回复
up !! 焦急等待ing…………

难道就没有哪位高手有解决这个问题的方法?
ATCG 2004-03-22
  • 打赏
  • 举报
回复
哪要12个开关,你可以设两个参数
test(mORq number,mypara number)
第一个参数如果为1,表示第二个参数为季度,
第一个参数如果为2,表示第二个参数为月份,
如test(1,1)
表示1季度
如test(2,1)
表示1月份

所以只要两个open语句就可以了

create or replace procedure test(qORm number,mypara varchar2)
as
type ref_cursor is ref cursor;
your_cursor ref_cursor;
var1 aa.name%type;
var2 varchar2(20);
m_beg aa.month%type;
m_end aa.month%type;
var3 varchar2(20);
begin
var2:=mypara;
select decode(var2,1,1,2,4,3,7,10) into m_beg from dual;
select decode(var2,1,3,2,6,3,9,12) into m_end from dual;
if qORm=1 then
open your_cursor for select name from aa where month>=m_beg and month<=m_end;
else
open your_cursor for select name from aa where month='1';
end if;
loop
fetch your_cursor into var1;
exit when your_cursor%notfound;
dbms_output.put_line(var1);
end loop;
close your_cursor;
end test;
/
zhangbat 2004-03-22
  • 打赏
  • 举报
回复
咳,如果没人回我就写开关去了
zhangbat 2004-03-22
  • 打赏
  • 举报
回复
只能用这种方法么?
咳,好累,要是用这种方法,我就得做一个 12 个条件的开关,然后把这个语句粘贴12遍……
ATCG 2004-03-22
  • 打赏
  • 举报
回复
如果,如果参数为1,表示1季度
则查询1、2、3月得数据
create or replace procedure test(mypara number)
as
type ref_cursor is ref cursor;
your_cursor ref_cursor;
var1 varchar2(8);
begin
if mypara=1 then
open your_cursor for select name from aa where month in(1,2,3);
else
open your_cursor for select name from aa where month='1';
end if;
loop
fetch your_cursor into var1;
exit when your_cursor%notfound;
dbms_output.put_line(var1);
end loop;
close your_cursor;
end test;
/

zhangbat 2004-03-22
  • 打赏
  • 举报
回复
我的代码如下,其中季累计的不知道怎么写了 strYearMonth 是传递过来的变量

-----------------------------------------------------------
strYearFirstMonth t_a.Yearmonth%type := substr(strYearMonth,1,4) || '01';
type Cur_RefHS is ref cursor ;
CurrefHS cur_refhs;
begin

if accumulationTypeID = 1 then
--月累计
open currefhs for
Select *
From t_b b,
t_a a
Where a.ID=b.ID
and b.yearmonth = strYearMonth;
elsif accumulationTypeID = 3 then
--年累计
open currefhs for
open currefhs for
Select *
From t_b b,
t_a a
Where a.ID=b.ID
and b.YearMonth >= stryearFirstMonth and ;
elsif accumulationTypeID = 2 then
--季累计
null;
end if;
zhangbat 2004-03-22
  • 打赏
  • 举报
回复
呵呵,终于有人回复了,

我用的就是游标变量,现在的问题是其中的一个条件不好搞。

为了它我还要把相似的代码写好几遍,并且其中一个 类似 in (……) 的还不大好写

17,086

社区成员

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

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