gpt4 book ai didi

sql - 声明变量时PLSQL中的存储过程变量错误

转载 作者:行者123 更新时间:2023-12-05 03:02:14 25 4
gpt4 key购买 nike

创建以下存储过程时使用 Oracle 11g

    create or replace PROCEDURE sp_EqualVote(AREA IN NVARCHAR2, DATEOFVOTE IN DATE)
IS
DECLARE test nvarchar(255);
BEGIN
SELECT
AREA,
DATEOFVOTE,
CASE
WHEN (REMAINVOTES = LEAVEVOTES) THEN REMAINVOTES
END AS EqualVote
INTO test
FROM VOTING
WHERE REMAINVOTES = LEAVEVOTES;
END;
END;

我遇到以下错误,我不太确定去哪里

PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior external language The symbol "begin" was substituted for "DECLARE" to continue.

我是一名大学生,对 PLSQL 不是很熟悉。这个想法是存储过程应该显示一个区域是否有相同的选票,给定过程中的区域和日期,然后显示一个 equalvotes 标记的列,值为 50

最佳答案

不少错误。

  • 你不需要DECLARE在命名的 PL/SQL 过程中
  • 参数名称应与列名称不同,因此您宁愿使用 - 例如 - p_area in nvarchar2, p_dateofvote in date
  • 如果你选择3列,你必须把它们放在INTO 3 个变量 - 你只声明了一个,所以要么再声明两个,要么删除 AREADATEOFOTE来自 SELECT
  • 这些参数有什么用?通常,作为 WHERE 的一部分子句 - 在您的代码中不是这种情况
  • 注意 SELECT 返回的行数陈述。如果您选择的是标量变量,请确保它只返回一行
  • 你会用TEST做什么变量,一旦你得到它的值(value)?目前,没有
  • 你有一个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>

关于sql - 声明变量时PLSQL中的存储过程变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55092045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com