Using variables in PLSQL SELECT statement

后端 未结 2 665
时光说笑
时光说笑 2020-12-11 20:29

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:

         


        
2条回答
  •  抹茶落季
    2020-12-11 20:56

    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 with PLS-00428: an INTO clause is expected in this SELECT statement error). So you'll have to rewrite your statements as below.

    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 
    

    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 a TOO_MANY_ROWS exception.

提交回复
热议问题