在 PLSQL SELECT 语句中使用变量
我有一个查询 ReportStartDate 和 ReportEndDate 的查询,所以我想我会在 PLSQL 中使用变量。不确定我在这里缺少什么,但我收到一个错误:
CLEAR;
DECLARE
varReportStartDate Date := to_date('05/01/2010', 'mm/dd/yyyy');
varReportEndDate Date := to_date('05/31/2010', 'mm/dd/yyyy');
BEGIN
SELECT
'Value TYPE',
1 AS CountType1,
2 AS CountType2,
3 AS CountType3
FROM DUAL;
SELECT COUNT (*)
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate
AND ReportEndDate = varReportEndDate
;
END;
/
错误是:
Error starting at line 2 in command:
Error report:
ORA-06550: line 6, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
这发生在 Toad 以及 SQL Developer 中。
在 WHERE 子句中使用变量的正确方法是什么?
I have a query that queries on ReportStartDate and ReportEndDate so I thought I would use variables in PLSQL. Not sure what I am missing here, but I get an error:
CLEAR;
DECLARE
varReportStartDate Date := to_date('05/01/2010', 'mm/dd/yyyy');
varReportEndDate Date := to_date('05/31/2010', 'mm/dd/yyyy');
BEGIN
SELECT
'Value TYPE',
1 AS CountType1,
2 AS CountType2,
3 AS CountType3
FROM DUAL;
SELECT COUNT (*)
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate
AND ReportEndDate = varReportEndDate
;
END;
/
The Error is:
Error starting at line 2 in command:
Error report:
ORA-06550: line 6, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This happens in Toad as well as in SQL Developer.
What is the proper way of using the variables in my WHERE clause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能直接在 PL/SQL 块中使用 SQL 语句(除非您使用 EXECUTE IMMEDIATE)。需要将列提取到变量中(这就是 PL/SQL 通过
PLS-00428: an INTO 子句预计在此 SELECT 语句中
错误告诉您的内容)。因此,您必须按如下方式重写您的陈述。请务必添加异常处理程序,因为 PL/SQL 预计仅返回 1 行。如果该语句没有返回任何行,您将遇到
NO_DATA_FOUND
异常 - 如果该语句获取太多行,您将遇到TOO_MANY_ROWS
异常。You cannot use SQL statements directly in a PL/SQL block ( unless you use
EXECUTE IMMEDIATE
). The columns will need to be fetched into variables ( which is what PL/SQL is telling you withPLS-00428: an INTO clause is expected in this SELECT statement
error). So you'll have to rewrite your statements as below.Be sure to add Exception Handlers, since PL/SQL expects only 1 row to be returned. If the statement returns no rows, you'll hit a
NO_DATA_FOUND
exception - and if the statement fetches too many rows, you'll hit aTOO_MANY_ROWS
exception.您必须回答的问题是您想要如何处理已选择的数据?
Sathya 为您提供了一种方法 - 在 PL/SQL 块中声明变量并选择这些变量的列
INTO
。请注意,这要求 SELECT 语句恰好返回一行 - 任何多或少的行都会引发错误。另一种方法是使用 BULK COLLECT 选项声明集合类型:http://oracletoday。 blogspot.com/2005/11/bulk-collect_15.html另一种选择是让过程返回游标。这在调用代码希望能够获取过程已选择的数据的情况下非常有用:
The question you have to answer is what do you want to do with the data that has been selected?
Sathya gave you one approach - declare variables in your PL/SQL block and select the columns
INTO
those variables. Note that this requires that the SELECT statement returns exactly one row - any more or less rows will throw an error. Another way is to declare collection types using the BULK COLLECT option: http://oracletoday.blogspot.com/2005/11/bulk-collect_15.htmlYet another option is to have the procedure return a cursor. This is useful in the case where the calling code expects to be able to fetch the data that the procedure has selected: