动态行转列的代码 编译不过,大家帮帮我啊,着急!

silinzi 2006-04-28 07:37:58
DECLARE
str varchar2(4000) := '';
begin
str := 'select id';
FOR v_day IN (SELECT DISTINCT to_char(PLAN_DATE,'yy-MM-dd') FROM tb) LOOP
str := str || ',sum(decode(to_char(PLAN_DATE,''yy-MM-dd''),' || v_day || ', REQ_QTY, 0)) ' || v_day;
END LOOP;
str := str || ' from tb group by id';
dbms_output.put_line(str);
end;

注:PLAN_DATE是日期型字段
编译时提示错误:调用'||'时参数个数或类型错误。
...全文
187 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaoxiulan 2006-04-29
  • 打赏
  • 举报
回复
DECLARE
str varchar2(4000) := '';
begin
str := 'select id';
FOR v_day IN (SELECT DISTINCT to_char(PLAN_DATE,'yy-MM-dd') s FROM tb) LOOP
str := str || ',sum(decode(to_char(PLAN_DATE,''yy-MM-dd''),' || v_day.s || ', REQ_QTY, 0)) ' || v_day.s;
END LOOP;
str := str || ' from tb group by id';
dbms_output.put_line(str);
end;
gaoxiulan 2006-04-29
  • 打赏
  • 举报
回复
同意楼上
bobfang 2006-04-29
  • 打赏
  • 举报
回复
DECLARE
str varchar2(4000) := '';
begin
str := 'select id';
FOR v_day IN (SELECT DISTINCT to_char(PLAN_DATE,'yy-MM-dd') s FROM tb) LOOP
str := str || ',sum(decode(to_char(PLAN_DATE,''yy-MM-dd''),' || v_day.s || ', REQ_QTY, 0)) ' || v_day;
END LOOP;
str := str || ' from tb group by id';
dbms_output.put_line(str);
end;
mschen 2006-04-28
  • 打赏
  • 举报
回复
--应该是你的循环语句有问题,使用游标就没问题了.

declare
v_str varchar2(4000):='';
v_day varchar2(20);
cursor c_Test is
select distinct to_char(Plan_Date,'yy-mm-dd') from tb;
begin
v_str:='select id';
open c_Test;
Loop
fetch c_Test into v_day;
exit when c_Test%NotFound;
v_str := v_str || ',sum(decode(to_char(PLAN_DATE,''yy-MM-dd''),' || v_day || ', REQ_QTY, 0)) ' || v_day;
End Loop;
close c_Test;
v_str :=v_str||' from tb group by id';
dbms_output.put_line(v_str);
end;

17,082

社区成员

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

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