【问题标题】:id invalid column in cursor though my table has column id尽管我的表有列 id,但光标中的 id 列无效
【发布时间】:2017-05-30 20:45:20
【问题描述】:

我的表有一个定义的列 ID。 虽然相同的查询独立运行良好,但是当我尝试在游标中声明它时,它显示无效列。

declare 
  type PA_1 is record (PI number); 
  calc number; 
  row_container PA_1; 
begin 
  for row_container in 
  (
     select distict t1.pi , t2.id 
     from table1 t1, table2 t2 
     where t1.Pi=t2.pi
  ); 
  Loop 
    select calculation to calc 
    from table1 t1 
    where t1.pi=row_container.pi and t2.id=row_container.id;
  end loop; 
  commit; 
end; 

否则内部查询运行良好。请帮忙

【问题讨论】:

  • declare type PA_1 is record (PI number); calc number; row_container PA_1; begin for row_container in (select distict t1.pi , t2.id from table1 t1, table2 t2 where t1.Pi=t2.pi); Loop select calculation to calc from table1 t1 where t1.pi=row_container.pi and t2.id=row_container.id; end loop; commit; end; 否则内部查询运行良好。请帮忙
  • 除了我在回答中显示的语法错误之外,您不应再使用这种旧的连接语法。它在 25 年前就被淘汰了。请改用from table1 t1 inner join table2 t2 on t1.Pi = t2.pi。
  • 顺便说一句,这个代码块应该做什么?许多任务可以用纯 SQL 编写,不需要 PL/SQL。
  • 嗨..请原谅我的语法错误..匆忙提交..代码块是一个巨大表更新的一部分..我想更新列“calc”使用我从加入中获得的值。虽然我可以通过将其声明为同义词来消除无列的“id”问题。我得到的“calc”值太多。 id 是唯一的列。我仍然得到太多的价值
  • 但是,如果这不是您遇到问题的实际代码,那么发布它是没有意义的。

标签: plsql oracle10g oracle-sqldeveloper


【解决方案1】:

几个语法错误:

  • 首先,您应该删除row_container 的声明。 for row_container in () 隐含地做到了这一点(您的一列声明甚至与您的两列查询都不匹配)。
  • distict 应该是 distinct。
  • 然后删除Loop 关键字之前的分号。它不属于那里。
  • 那么select calculation to calc应该是select calculation into calc。
  • 在循环中,您从table1(您再次调用t1)中选择,但您的where 子句包含t2.id,而该查询中没有t2。

然后:这个例程应该做什么?它在变量calc 中选择一些值,但不使用它。所以一旦它运行,它就什么都不做。

【讨论】:

    猜你喜欢
    • 2016-01-12
    • 2017-01-08
    • 2023-03-18
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2015-02-19
    相关资源
    最近更新 更多