0

为什么我在运行以下行时收到循环变量索引 (i) 的“无效标识符”错误消息?

SQL 错误:ORA-00904:“I”:标识符无效

for i in 1..5 loop
   select Price from Orders where (ID=i);
end loop;

Orders是一个包含两个字段的表:ID 和价格。

表顺序:

身份证价格
------------
1 1200    
2 2250    
3 1750    
4 2000    
5 1000    
6 300    
4

2 回答 2

1

您可能需要将其作为匿名块运行?这是一个如何使循环工作的简单示例:

DECLARE
  v_last_name VARCHAR2(2000);

BEGIN
  for i in 1..5 loop

    WITH sample_set AS (
      SELECT 1 the_number, 'frank' last_name FROM DUAL UNION ALL
      SELECT 2 the_number, 'STEVE' last_name FROM DUAL UNION ALL
      SELECT 3 the_number, 'CARL' last_name FROM DUAL UNION ALL
      SELECT 4 the_number, 'bella' last_name FROM DUAL UNION ALL
      SELECT 5 the_number, 'tank' last_name FROM DUAL)
    select last_name
    INTO   v_last_name
    from sample_set where the_number = i;

    dbms_output.put_line(v_last_name);

  end loop;
END;
于 2013-10-25T15:09:20.000 回答
0

这样做

DECLARE 
my_price long;
BEGIN
FOR I IN 1..5 loop
SELECT PRICE INTO MY_PRICE FROM ORDERS where id=i;
DBMS_OUTPUT.PUT_LINE('The price is'||' '||my_price);
end loop;
end;

OUTPUT
-------------------------------
The price is 1234
The price is 1235
The price is 1236
The price is 1237
The price is 1238

Statement processed.
于 2013-11-11T11:03:23.250 回答