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

Luk*_*ood 4 sql oracle plsql stored-procedures oracle11g

创建以下存储过程时使用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;
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

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

Lit*_*oot 5

错误还蛮多的。

  • 您不需要DECLARE在指定的 PL/SQL 过程中
  • 参数名称应与列名称不同,因此您宁愿使用 - 例如 -p_area in nvarchar2, p_dateofvote in date
  • 如果您选择 3 列,则必须将它们放入INTO3 个变量 - 您只声明了一个,因此要么声明另外两个,要么AREADATEOFOTESELECT
  • 这些参数有什么用?通常,作为子句的一部分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>
Run Code Online (Sandbox Code Playgroud)

[编辑]

阅读完评论后,也许您更愿意使用函数。

一些示例值:

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.
Run Code Online (Sandbox Code Playgroud)

功能:

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>
Run Code Online (Sandbox Code Playgroud)

测试:

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>
Run Code Online (Sandbox Code Playgroud)