不少错误。
- 在命名的 PL/SQL 过程中不需要
DECLARE
- 参数名称应该与列名称不同,因此您宁愿使用 - 例如 -
p_area in nvarchar2, p_dateofvote in date
- 如果选择 3 列,则必须将它们放入
INTO 3 个变量 - 您只声明了一个,所以要么再声明两个,要么从 SELECT 中删除 AREA 和 DATEOFOTE
- 这些参数是做什么用的?通常,作为
WHERE 子句的一部分 - 您的代码中并非如此
- 注意
SELECT 语句返回的行数。如果您选择标量变量,请确保它只返回一行
- 一旦你获得了
TEST 变量的值,你将如何处理它?目前,什么都没有
- 你有一个
END,这是一个盈余。
因此,考虑这样的事情应该至少编译(取决于表描述):
SQL> create table voting (area nvarchar2(10),
2 dateofvote date,
3 remainvotes nvarchar2(10),
4 leavevotes nvarchar2(10));
Table created.
SQL> create or replace procedure
2 sp_equalvote(p_area in nvarchar2, p_dateofvote in date)
3 is
4 test nvarchar2(255);
5 begin
6 select
7 case when remainvotes = leavevotes then remainvotes end
8 into test
9 from voting
10 where remainvotes = leavevotes
11 and area = p_area
12 and dateofvote = p_dateofvote;
13 end;
14 /
Procedure created.
SQL>
[编辑]
看完评论,也许你更愿意用一个函数。
一些样本值:
SQL> insert into voting values (1, date '2019-02-20', 100, 15);
1 row created.
SQL> insert into voting values (1, date '2019-03-10', 300, 300);
1 row created.
功能:
SQL> create or replace function
2 sp_equalvote(p_area in nvarchar2, p_dateofvote in date)
3 return nvarchar2
4 is
5 test nvarchar2(255);
6 begin
7 select
8 case when remainvotes = leavevotes then 'draw'
9 else 'not equal'
10 end
11 into test
12 from voting
13 where area = p_area
14 and dateofvote = p_dateofvote;
15
16 return test;
17 end;
18 /
Function created.
SQL>
测试:
SQL> select * From voting;
AREA DATEOFVOTE REMAINVOTE LEAVEVOTES
---------- ---------- ---------- ----------
1 20.02.2019 100 15
1 10.03.2019 300 300
SQL> select sp_equalvote(1, date '2019-02-20') res from dual;
RES
--------------------
not equal
SQL> select sp_equalvote(1, date '2019-03-10') res from dual;
RES
--------------------
draw
SQL>