Oracle 简单存储过程的问题。。。 在线等。。。

Ghost_520 2011-06-15 04:50:41

create or replace procedure p_complte_d_rank(filterStr varchar2 , p_period varchar2) is
type c_total is ref cursor;
v_total c_total; -- 游标记录每行值
v_unitid SGCC_FACT_COMPLETE_D.unit_id%type; -- 单位ID
v_total_value number; -- 总得分
v_rank number ; -- 排名
begin

open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= p_period and unit_id = filterStr; -- 这样写就没有值循环

-- select unit_id from sgcc_fact_complete_d
-- where sj_type= '201104' and unit_id = '501101010000009112'; 这样写就有值循环
LOOP
fetch v_total into v_unitid ;
exit when v_total%notfound;

dbms_output.put_line(v_unitid);
end LOOP ;
end p_complte_d_rank;



我来描述下问题, 我的输入参数是 '201104' ,'501101010000009112' , 如果我用输入参数来代替 p_period , filterStr

的值, 就没有值循环, 如果我直接把时间 , 和单位编号写固定, 就有值可以进行循环, 请问各位大神, 这是什么原因

造成的, 小弟不才, 在线等 。。。。。
...全文
141 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ghost_520 2011-06-16
  • 打赏
  • 举报
回复
感觉还是糊里糊涂的, 先结贴了。
hudingchen 2011-06-16
  • 打赏
  • 举报
回复
你使用in的方式:
select * from where unit_id in (501101010000009112,501101010000009113,501101010000009111)

等价于:
select * from where and instr(',501101010000009112,501101010000009113,501101010000009111,',',' || unit_id || ',') > 0
意思是字符串',501101010000009112,501101010000009113,501101010000009111,'中包含',unit_id,',加逗号是为了确定你传递参数中查询的条件,比如:',501101010000009112,'表示501101010000009112是你的检索条件。
Ghost_520 2011-06-16
  • 打赏
  • 举报
回复
11 楼, 我像你那 open 游标, 报错了。
Ghost_520 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hudingchen 的回复:]
我写的那个没用动态sql,你没懂哦,方法写错了,改下

SQL code
open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= ''||p_period||'' and instr(',' || filterStr || ',',',' || unit_id || ',') > 0
;……
[/Quote]


这种方式可以, 能否解释下原因啊, 谢谢
shxwrgc110 2011-06-15
  • 打赏
  • 举报
回复
open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= "||p_period||" and unit_id in ("|| REPLACE( '||filterStr||',',','","' ))
);

这个尝试一下
dyeerp 2011-06-15
  • 打赏
  • 举报
回复
很好。
hudingchen 2011-06-15
  • 打赏
  • 举报
回复
我写的那个没用动态sql,你没懂哦,方法写错了,改下
open v_total for 
select unit_id from sgcc_fact_complete_d
where sj_type= ''||p_period||'' and instr(',' || filterStr || ',',',' || unit_id || ',') > 0
;
Ghost_520 2011-06-15
  • 打赏
  • 举报
回复
我的 sql 非常长啊, 如果用 动态 拼 sql 的 话, 那拼到明年去了。
kakatian 2011-06-15
  • 打赏
  • 举报
回复
看不懂
hudingchen 2011-06-15
  • 打赏
  • 举报
回复
open v_total for 
select unit_id from sgcc_fact_complete_d
where sj_type= ''||p_period||'' and substr(',' || filterStr || ',',',' || unit_id || ',') > 0
;

如果用in就得动态sql了.
Ghost_520 2011-06-15
  • 打赏
  • 举报
回复
回复楼上的, 确实单引号的问题, 我刚才也成功试出来了, 但是有个问题, 如果 filterStr 这个字段是

多个值组成的, 例如 501101010000009112,501101010000009113,501101010000009111 , 那这个单

引号要怎么写? 我修改成如下了


open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= ''||p_period||'' and unit_id = ''||filterStr ||'';


多个值的话。
open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= ''||p_period||'' and unit_id in (这里要怎么用单引号
);

tangren 2011-06-15
  • 打赏
  • 举报
回复
--改成这样
CREATE OR REPLACE PROCEDURE p_complte_d_rank(filterStr VARCHAR2,
p_period VARCHAR2) IS
TYPE c_total IS REF CURSOR;
v_total c_total; -- 游标记录每行值
v_unitid SGCC_FACT_COMPLETE_D.unit_id%TYPE; -- 单位ID
v_total_value NUMBER; -- 总得分
v_rank NUMBER; -- 排名
BEGIN

OPEN v_total FOR 'SELECT unit_id
FROM sgcc_fact_complete_d
WHERE sj_type = ''' || p_period || ''' AND unit_id = ''' || filterStr || '''';
LOOP
FETCH v_total
INTO v_unitid;
EXIT WHEN v_total%NOTFOUND;

dbms_output.put_line(v_unitid);
END LOOP;
END p_complte_d_rank;
hudingchen 2011-06-15
  • 打赏
  • 举报
回复
你是怎么调用这个存储过程的?
Ghost_520 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bobo12082119 的回复:]
--将游标查询放在定义部分试试
open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= p_period and unit_id = filterStr;
begin
...
[/Quote]

按照你的意思, 我改成如下, 还是不行呢, 没有值进行循环

create or replace procedure p_complte_d_rank(filterStr varchar2 , p_period varchar2) is
v_unitid SGCC_FACT_COMPLETE_D.unit_id%type; -- 单位ID
v_total_value number; -- 总得分
v_rank number ; -- 排名
cursor c_total is select unit_id from sgcc_fact_complete_d
where sj_type= p_period and unit_id = filterStr;
begin
for data in c_total
loop
dbms_output.put_line('111');
end loop;
end p_complte_d_rank;
304的的哥 2011-06-15
  • 打赏
  • 举报
回复
--将游标查询放在定义部分试试
open v_total for
select unit_id from sgcc_fact_complete_d
where sj_type= p_period and unit_id = filterStr;
begin
...
相当不错的技术文档。 前言 1 第一章 oracle存储过程概述 2 1.1 存储过程基本结构(PROCEDURE) 3 1.1.1创建存储过程 3 1.1.2 存储过程删除 5 1.1.3 调用存储过程 5 1.2存储函数(FUNCTIONE) 6 1.2.1 创建存储函数 6 1.2.2 删除存储函数 7 1.3 包(package) 7 1.3.1 包的基本结构 7 1.3.2 包的创建 7 1.3.3 调用包中元素 9 1.3.4 包的修改和删除 9 第二章 oracle存储过程基础――PL/SQL 9 2.1 pl/sql基础 9 2.1.1 PL/SQL简介 9 2.1.2 一个简单的PL/SQL块 10 2.1.3 PL/SQL流程控制 13 2.2 游标(CURSOR) 17 2.2.1 游标的概念 18 2.2.2 游标的属性 18 2.2.3 游标中FOR循环的使用 20 2.2.4 带参数游标的使用方法 20 2.3 动态SQL语句 21 2.4 例外处理 22 2.5 一个完整的PL/SQL实例 24 第三章 oracle存储过程讨论 25 3.1 函数(FUNCTION) 26 3.1.1 用户函数创建,编译,删除 26 3.1.2 参数传递 27 3.2 存储过程 28 3.3 包 29 3.3.1 创建包 30 3.3.2 删除包 30 3.3.3 应用举例 31 3.4 UTL_FILE包的使用 33 3.4.1 文件控制: 34 3.4.2 文件输出: 34 3.4.3 文件输入: 35 3.4.4 应用举例 35 4.1 Wrapper应用 35 第四章 存储过程运行环境 36 4.1 存储过程以及PL/SQL执行环境 36 4.1.1 SQL*PLUS环境 36 4.1.2 Pro*c预编译环境 37 4.2 存储过程调试方法 38 4.2.1 SQL*PLUS环境中显示错误 38 4.2.2 插入测试表调试存储过程 38 4.2.3 DBMS_OUTPUT系统内置包 39 附录一 sql*plus工具 40 附录1.1 sql*plus启动和关闭 41 附录1.2 sql*plus 环境设置 42 附录1.3 设置环境参数 42 附录1.4 sqlplus命令的执行 43 附录1.5 sql*plus编辑命令 43

17,086

社区成员

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

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