日期:2014-05-17  浏览次数:21181 次

oracle存储过程into赋值,子查询等,来帮我看看
jianju number(20,4);变量

for r in (select * from( select * from tb_points where district_name='XMQ' and location_name='上行' and pyear='2003' and pmonth='04' order by milestone)where rownum=1) loop --没有问题

   
  select * from (select milestone-r.milestone into jianju from tb_points where district_name=r.district_name and location_name=r.location_name and pyear=r.pyear and pmonth=r.pmonth and milestone > r.milestone order by milestone) where rownum=1 ; --有问题

红色地方into jianju 变量赋值。

第二红色地方 先milestone排序后, 取第一行。

  dbms_output.put_line(jianju);输出,

Error: PL/SQL: ORA-01744: 不合理的 INTO 报错。

  或者有什么方法 先排序好,然后取第一行,用into 取值。
  或者有什么方法 for循环 r 怎么取下一条记录。




------解决方案--------------------
rownum=1放括号里面去用and连接,不知道可以不
------解决方案--------------------
搞不明白lz想干嘛,你是想查数据呢,还是插数据。
------解决方案--------------------
还是没太明白LZ的意思,不过游标应该可以这样取值
------解决方案--------------------
DECLARE
jianju NUMBER(20,4) := 0;--变量
dist number(20,4) := 0;
  
strSql varchar2(1000) := '';
BEGIN
--按milestone排序后,
FOR r IN
(SELECT *
FROM tb_points
WHERE district_name='XMQ'
AND location_name ='上行'
AND pyear ='2003'
AND pmonth ='04'
ORDER BY milestone
)
LOOP --没有问题
dist := jianju;
jiangju := r.milestone;
dist := jiangju - dist;
--dist就是需要的两条记录之间的间距
--这里写INSERT INTO 语句
/*
--例如下写法
strSql := 'insert into tableOther (milestone) values (' || to_char(jianju) || ')'
execute immediate strSql;
commit;
*/
END LOOP;
END;

--附:如何仅仅是取与上一条记录的差值,可以直接用LAG函数写出查询结果。



------解决方案--------------------
SQL code
select aaa into jianju  from (select milestone-r.milestone aaa  from tb_points where district_name=r.district_name and location_name=r.location_name and pyear=r.pyear and pmonth=r.pmonth and milestone > r.milestone order by milestone) where rownum=1