我需要运行一个查询,找到前20辆与特定条款相近的车辆。我是在Oracle Developer的oracle数据库上写的。
这是我到目前为止所拥有的:
DECLARE
TYPE cur_type IS REF CURSOR;
CURSOR lost_vehicles_cur IS
select
v.vin,
V.VEHICLE_ID
from d_vehicles V
where v.system_id =4 ;
make_cur cur_type;
l_cur_string VARCHAR2(2000);
l_make <type>;
l_model <type>;
l_vin <type>;
BEGIN
FOR vehicle IN lost_vehicles_cur LOOP
dbms_output.put_line('lost vehicle is '|| vehicle.vehicle_id);
l_cur_string := 'SELECT make_name, model_name,vin FROM vehicles where make=(select make from vehicles where vehicle_id= '
|| vehicle.vehicle_id || 'and rownum<=20 and system_id=3 and vehicle_status_id in (13,14) ';
OPEN make_cur FOR l_cur_string;
LOOP
FETCH make_cur INTO l_make, l_model, L_vin;
EXIT WHEN make_cur%NOTFOUND;
dbms_output.put_line('Related vehicles are ' || l_make || l_model || L_vin);
END LOOP;
CLOSE make_cur;
END LOOP;
END;
我使用了之前的答案来解决这个问题,但是当我运行它时出现以下错误:
ORA-06550:第32行,第13栏:PLS-00103:遇到符号“&lt;”当期望以下之一时:常量异常表long double ref char time timestamp interval date二进制国家字符nchar 06550. 00000 - “line%s,column%s:\ n%s”*原因:通常是PL / SQL编译错误。 -
答案 0 :(得分:1)
您必须使用参数化游标或dbms_sql:
DECLARE
TYPE cur_type IS REF CURSOR;
CURSOR lost_vehicles_cur IS
select
v.vin,
V.VEHICLE_ID
from d_vehicles V
where v.system_id =4 ;
cursor l_cur (ip_id in d_vehicles.VEHICLE_ID%type)
SELECT make_name, model_name,vin
FROM vehicles
where make in (select make
from vehicles
where vehicle_id= ip_id
and rownum<=20
and system_id=3
and vehicle_status_id in (13,14));
make_cur cur_type;
l_cur_string VARCHAR2(2000);
l_make <type>;
l_model <type>;
l_vin <type>;
BEGIN
FOR vehicle IN lost_vehicles_cur LOOP
dbms_output.put_line('lost vehicle is '|| vehicle.vehicle_id);
OPEN make_cur FOR l_cur (vehicle.vehicle_id);
LOOP
FETCH make_cur INTO l_make, l_model, L_vin;
EXIT WHEN make_cur%NOTFOUND;
dbms_output.put_line('Related vehicles are ' || l_make || l_model || L_vin);
END LOOP;
CLOSE make_cur;
END LOOP;
END;