-2

Pleas help me to resolve this error in plsql.

 sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = 'INACTIVE' AND LAST_CALL_ET > 0';
 EXECUTE IMMEDIATE sqlquery into s_count;

below is the error:

  ERROR at line 57:
  ORA-06550: line 57, column 66:
  PLS-00103: Encountered the symbol "INACTIVE" when expecting one of the following:
  * & = - + ; < / > at in is mod remainder not rem
  <an exponent (**)> <> or != or ~= >= <= <> and or like like2
  like4 likec between || multiset member submultiset
  The symbol "* was inserted before "INACTIVE" to continue.
  ORA-06550: line 79, column 4:
  PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
  ( begin case declare else elsif end exit for goto if loop mod
  null pragma raise return select update while with <an identifier> <a double-quoted
  ORA-06550: line 81, column 7:
  PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
  end not pragma final instantiable order overriding static member constructor map
user272735
  • 10,473
  • 9
  • 65
  • 96
user3422419
  • 21
  • 1
  • 10
  • Does this really need to be done with an `EXECUTE IMMEDIATE`? The query isn't dynamic, so you should be able to implement this with a standard cursor and an `OPEN`, `FETCH`, `CLOSE` or a `SELECT INTO` – Rob Baillie Apr 11 '14 at 11:48
  • This is essentially the same issue than e.g. http://stackoverflow.com/a/22810334/272735 – user272735 Apr 11 '14 at 11:59
  • 1
    This question appears to be off-topic because it is about a simple typo. – Jeffrey Kemp Apr 11 '14 at 13:47
  • Also same as https://stackoverflow.com/questions/48172315/wrong-symbol-inside-replace-function-pl-sql-oracle/48172423 – William Robertson Feb 26 '18 at 15:23

10 Answers10

11

The quotes around INACTIVE are splitting the string up and causing the syntax error...

sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = ''INACTIVE'' AND LAST_CALL_ET > 0';
EXECUTE IMMEDIATE sqlquery into s_count;

Of course, if the statement isn't actually dynamic you don't need the EXECUTE IMMEDIATE and can instead go with:

SELECT COUNT(*)
INTO   s_count
FROM   V$SESSION
WHERE  STATUS = 'INACTIVE'
AND    LAST_CALL_ET > 0';
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
1
set serveroutput on;
declare
   l_status varchar2(30):= 'INACTIVE';
   s_count number:= 0;
sqlquery varchar2(32767) := null;
begin
sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = :a  AND LAST_CALL_ET > 0';
EXECUTE IMMEDIATE sqlquery into s_count using l_status ;
dbms_output.put_line(s_count);
end;
1

Use the following query:

sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = ''INACTIVE'' AND LAST_CALL_ET > 0';
jherran
  • 3,337
  • 8
  • 37
  • 54
0

Solution with Execute immediate

set serveroutput on;
declare
   l_status varchar2(30):= '''INACTIVE''';
   s_count number:= 0;
sqlquery varchar2(32767) := null;
begin
sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = '||l_status||' AND LAST_CALL_ET > 0';
EXECUTE IMMEDIATE sqlquery into s_count;
dbms_output.put_line(s_count);
end;
0
EXECUTE IMMEDIATE '
SELECT COUNT(*) 
FROM V$SESSION 
WHERE STATUS = ''INACTIVE'' AND LAST_CALL_ET > 0' 
INTO s_count;
touchchandra
  • 1,506
  • 5
  • 21
  • 37
0

Please change the query as sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = '''INACTIVE''' AND LAST_CALL_ET > 0'; EXECUTE IMMEDIATE sqlquery into s_count;

Arun nath
  • 81
  • 1
  • 4
0

Bind variables like Chandra said!

One way I like to assign the SQL:

declare
  vSQL varchar2( 100 ) ;
begin
  vSQL := q'[select 'done' from dual ]' ;
  dbms_output.put_line( vSQL ) ;
end ;

so you'd do:

sqlquery := q'[SELECT COUNT(*) FROM V$SESSION WHERE STATUS = :status AND LAST_CALL_ET > 0]';
0

use :

sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = ''INACTIVE'' AND LAST_CALL_ET > 0';

not :

sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = 'INACTIVE' AND LAST_CALL_ET > 0';

Chakib Arrama
  • 93
  • 3
  • 12
0

use this code will work perfectly

set serveroutput on
    DECLARE
    sqlquery    varchar2(1000);
    INACTIVE    varchar2(100):='INACTIVE';
    s_count     number;
    begin
    sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = '''||INACTIVE||''' AND LAST_CALL_ET > 0';
    EXECUTE IMMEDIATE sqlquery into s_count;
    DBMS_OUTPUT.PUT_LINE(s_count);
    end;
    /
0

Another way to do this:

            set serveroutput on;
            DECLARE
                sqlquery VARCHAR2(4000);
                s_count number;
            begin
                sqlquery := 'SELECT COUNT(*) FROM V$SESSION WHERE STATUS = '||chr(39)||'INACTIVE'||chr(39)||' AND LAST_CALL_ET > 0';
                EXECUTE IMMEDIATE sqlquery into s_count;
                dbms_output.put_line('s_count--'||s_count);
            end;
            /
sudhirkondle
  • 127
  • 1
  • 5