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

时间:2019-03-10 20:25:39

标签: sql oracle stored-procedures plsql oracle11g

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

// the Data you want to share as a file
let data = Data()

// Write the data into a filepath and return the filepath in NSURL
// Change the file-extension to specify the filetype (.txt, .json, .pdf, .png, .jpg, .tiff...)
let fileURL = data.dataToFile(fileName: "nameOfYourFile.extension")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of the file to the Array
filesToShare.append(fileURL!)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)

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

    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;

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

2 个答案:

答案 0 :(得分:2)

很多错误。

  • 在命名的PL / SQL过程中不需要DECLARE
  • 参数名称应与列名称不同,所以您宁愿使用-例如p_area in nvarchar2, p_dateofvote in date
  • 如果您选择3列,则必须将它们放入INTO 3个变量中-您仅声明了一个变量,因此可以再声明两个变量,或者从{中删除AREADATEOFOTE {1}}
  • 这些参数是做什么用的?通常,作为SELECT子句的一部分-您的代码中不是这种情况
  • 注意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>

答案 1 :(得分:1)

PL / SQL过程主体中不允许

DECLAREISAS的作用是确定变量声明部分的开始位置-因此您的过程应该是

create or replace PROCEDURE sp_EqualVote(AREA IN NVARCHAR2, DATEOFVOTE IN DATE)
IS
  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,我已将其删除。

好运。