Using Variables in a PLS SELS Expression

I have a query that both ReportStartDate and ReportEndDate request, so I thought I would use variables in PLSQL. Not sure what I am missing here, but I get an error message:

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:

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 correct way to use variables in my WHERE clause?

+3
source share
2 answers

SQL PL/SQL ( EXECUTE IMMEDIATE). ( , PL/SQL PLS-00428: an INTO clause is expected in this SELECT statement). , .

SELECT 
      'Value TYPE', 
      1 AS CountType1, 
      2 AS CountType2, 
      3 AS CountType3 
INTO 
     V_VALUE_TYPE,
     V_CountType1,
     V_CountType2,
     V_CountType3
FROM DUAL;

SELECT COUNT(*) 
   INTO V_COUNT    
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate 
AND ReportEndDate = varReportEndDate 

, PL/SQL . , NO_DATA_FOUND - , TOO_MANY_ROWS.

+7

, , - , ?

: PL/SQL INTO . , , SELECT - . - BULK COLLECT: http://oracletoday.blogspot.com/2005/11/bulk-collect_15.html

, . , , , :

PROCEDURE GET_MY_REPORT( varReportStartDate in date,  varReportEndDate in date, cur out sys_refcursor) is
begin
   OPEN cur FOR SELECT * 
     FROM CDR.MSRS_E_INADVCH
     WHERE 1=1
     AND ReportStartDate = varReportStartDate 
     AND ReportEndDate = varReportEndDate;
END GET_MY_REPORT;
+4

Source: https://habr.com/ru/post/1777476/


All Articles