读书人

存储过程循环 大家伙儿帮忙调下

发布时间: 2013-03-26 09:54:34 作者: rapoo

存储过程循环 大家帮忙调下
create or replace procedure tj_count_coupon

as
a number;
v_limitid number;
v_limitname varchar2;
v_merchantname varchar2;

Begin
select count(*) into a
from couponinfo t
left join coupon_merchant t1 on t.coupon_id = t1.product_id
left join merchant t2 on t1.merchant_id = to_char(t2.merchant_id)
and t1.product_type = 1
where to_date(t.end_date, 'yyyy-mm-dd HH24:mi:ss') >= sysdate
and to_char(t.str_date, 'yyyy-mm-dd HH24:mi:ss' ) <= to_char(sysdate,'yyyy-mm-dd HH24:mi:ss')


for i in 1...a loop

select t2.name into v_merchantname,
t.coupon_id into v_limitid ,
t.coupon_name into v_limitname
from couponinfo t
left join coupon_merchant t1 on t.coupon_id = t1.product_id
left join merchant t2 on t1.merchant_id = to_char(t2.merchant_id)
and t1.product_type = 1
where to_date(t.end_date, 'yyyy-mm-dd HH24:mi:ss') >= sysdate
and to_char(t.str_date, 'yyyy-mm-dd HH24:mi:ss' ) <= to_char(sysdate,'yyyy-mm-dd HH24:mi:ss')
where rownum=i

insert into count_coupon((id, coupon_id, coupon_name, rec_date, down_count, merchant_name) values
(SEQ_count_coupon_ID.Nextval,v_limitid,v_limitname,to_char(sysdate,'yyyy-mm-dd'),
(select count(*) as t from user_coupon where coupon_id=v_limitid and to_char(reg_date,'yyyy-MM-dd')=to_char(sysdate,'yyyy-mm-dd')),


v_merchantname
);
end loop;


End tj_count_coupon;

[解决办法]

引用:
以前没写过存储过程 ! 比较初级 ! 刚刚调通了。但是测试的时候

这报错 !

你这不是没写过,就是不会写,不要强调自己其他方面的经验,有开发经验的人表现成这个样子,我忍不了了。
[解决办法]
引用:
引用:
以前没写过存储过程 ! 比较初级 ! 刚刚调通了。但是测试的时候

这报错 !
为啥会返回多条呢 加了 AND ROWNUM=I 不是应该返回一条么?

rownum不能这么用的,ROWNUM只适用于小于或小于等于。
为什么看这里http://blog.csdn.net/fw0124/article/details/6692022
你这种情况可以用游标来实现。

读书人网 >oracle

热点推荐