我创建了一个程序
create or replace procedure availability(num in number) as
begin
delete from vehicle_count;
insert into vehicle_count from select engine_no,count(engine_no)
from vehicle
where engine_no = num
group by engine_no;
end;
/
该过程已成功创建,但现在我必须编写单独的查询以查看vehicle_count的内容为
select * from vehicle_count;
我尝试在插入后将select语句插入到过程中,但是它显示错误,说明"在select语句中需要一个INTO子句"。
如何创建过程来选择所需内容并将其显示在单个执行语句中?
表架构
车辆(vehicle_no,engine_no,offence_count,license_status,owner_id); vehicle_count(engine_no,engine_count);
答案 0 :(得分:1)
选中此项(MS SQL SERVER) -
create or alter procedure availability(@num as int) as
begin
delete from vehicle_count;
insert into vehicle_count
output inserted.engine_no,inserted.count_engine_no
select engine_no,count(engine_no) as count_engine_no
from vehicle
where engine_no=@num
group by engine_no;
end;
答案 1 :(得分:1)
如果要将SELECT用于PL / SQL块,则应使用SELECT INTO或循环(如果要打印更多行)。 你可以使用这样的东西:
BEGIN
SELECT engine_no, engine_count
INTO v_engine, v_count
FROM vehicle_count
WHERE engine_no = num;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_engine := NULL;
v_count := NULL;
END;
v_engine和v_count是两个变量。您可以在程序中声明它们,它们将包含您要打印的值。
答案 2 :(得分:1)
您说您编写的程序(实际上,您已在此处发布)已成功编译。嗯,很遗憾地通知你 - 那不是真的。这不是有效的语法:
insert into vehicle_count from select engine_no,count(engine_no)
----
from? Here?
考虑发布真实信息。
截至你的问题(如果我们假设INSERT实际上将某些插入到表中):
create or replace procedure availability (num in number) as
l_engine_no vehicle_count.engine_no%type;
l_engine_count vehicle_count.engine_count%type;
begin
delete from vehicle_count;
insert into vehicle_count (engine_no, engine_count)
select engine_no, count(engine_no)
from vehicle
where engine_no = num
group by engine_no;
-- This query shouldn't return TOO-MANY-ROWS if ENGINE_NO is a primary key.
-- However, it might return NO-DATA-FOUND if there's no such NUM there, so you should handle it
select engine_no, engine_count
into l_engine_no, l_engine_count
from vehicle_count
where engine_no = num;
dbms_output.put_line(l_engine_no ||': '|| l_engine_count);
exception
when no_data_found then
dbms_output.put_line('Nothing found for ENGINE_NO = ' || num);
end;
/
有很多替代方案(在此之前发布其答案/评论的人提到了其中一些),并且您满意的最终结果取决于您希望显示该信息的位置。