-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
4
  • 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 Commented Apr 11, 2014 at 11:48
  • This is essentially the same issue than e.g. stackoverflow.com/a/22810334/272735 Commented Apr 11, 2014 at 11:59
  • 1
    This question appears to be off-topic because it is about a simple typo. Commented Apr 11, 2014 at 13:47
  • Also same as stackoverflow.com/questions/48172315/… Commented Feb 26, 2018 at 15:23

10 Answers 10

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';
Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

Please explain your answer
in sqlquery variable , you can see :a is used as binding variable. Later on those value is passed while "EXECUTE IMMEDIATE sqlquery into s_count using l_status ;" by using l_status. It is just similar like preparedStatment in java.
it is also safe comparing to traditional string concatenation.
1

Use the following query:

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

Comments

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;

Comments

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

Comments

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;

Comments

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]';

Comments

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';

Comments

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;
    /

Comments

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;
            /

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.