Solved

ORA-00936 using a SELECT INTO statement with Variables

Posted on 2012-04-03
6
984 Views
Last Modified: 2012-04-03
I can't have not been able to determine why the below statements fail. They run fine when the table name is hard coded, here are the statement:

select max(etl_aud_id) into v_etl_aud_id from prov_avg_cst_stg;

select  cst_sched_id into v_cst_sched_id from prov_avg_cst_stg
   where
   etl_aud_id=v_etl_aud_id
   group by cst_sched_id;

but as a parameter it fails to execute. Any ideas?

Get ORA-00936: missing expression
ORA-06512: at "HCCEETL.SP_TEMP_UPDATE1", line 13
ORA-06512: at line 7

Here's the procedure:

CREATE OR REPLACE PROCEDURE HCCEETL.sp_temp_update1(tbl_in in varchar2)
AS
v_sql varchar2(5000);
v_etl_aud_id number(10);
v_line1 varchar2(40);
v_cst_sched_id varchar2(40);

BEGIN
-- Get audit ID of records in staging table

   v_sql := 'SELECT max(etl_aud_id) INTO' || v_etl_aud_id|| ' FROM ' ||tbl_in;

execute immediate v_sql;
   
    v_line1 := 'Audit ID: '||v_etl_aud_id;
    dbms_output.put_line (v_line1);

v_sql := null;

 v_sql := 'SELECT CST_SCHED_ID INTO '
 ||V_CST_SCHED_ID||' FROM '
 ||TBL_IN|| ' WHERE ETL_AUD_ID='
 || V_ETL_AUD_ID
 ||'GROUP BY CST_SCHED_ID';

EXECUTE IMMEDIATE v_sql;

   v_line1 :='Cost Sched ID: '|| v_cst_sched_id;
    dbms_output.put_line (v_line1);

    commit;
    end;
/
0
Comment
Question by:SharonBernal
6 Comments
 
LVL 20

Expert Comment

by:gatorvip
ID: 37803509
>>'SELECT max(etl_aud_id) INTO'

Looks like you're missing a space after INTO
0
 

Author Comment

by:SharonBernal
ID: 37803581
I tried fixing the space. Get the same error missing expression.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 37803633
Try:
  v_sql := 'SELECT max(etl_aud_id) FROM ' ||tbl_in;

  EXECUTE IMMEDIATE v_sql INTO v_etl_aud_id;

Open in new window

:p
PS: Execute immediate will execute native sql and  'INTO' is PL/SQL.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 68

Expert Comment

by:Qlemo
ID: 37803702
Certainly it doesn't make sense to concat the value of the var into the SQL after INTO.
  v_sql := 'SELECT max(etl_aud_id) INTO' || v_etl_aud_id|| ' FROM ' ||tbl_in;
will resolve to something along
  v_sql := 'SELECT max(etl_aud_id) INTO FROM ' ||tbl_in;
since v_etl_aud_id will be NULL at that point.

The execute immediate as shown in http:#a37803633 is correct; the result var is not part of the dynamic SQL but the PL/SQL command, as stated.
0
 
LVL 20

Expert Comment

by:flow01
ID: 37803721
Tough I prefer the solution of MikeOM_DBA using native sql
you can also use a pl/sql block in the execute immediate

example:

declare
  v_statement varchar2(255);
  v_result varchar2(100);
begin
  v_statement := 'begin select to_char(sysdate,''dd/mm/yyyy'') into :rsl from dual;end;';
  execute immediate v_statement using out v_result;
  dbms_output.put_line(v_result);
end;
/
0
 

Author Closing Comment

by:SharonBernal
ID: 37803833
This works, I see the logic of getting the value first and then assigning it to a variable. Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle query help 29 77
I need to be able to return multiple values in my SQL case statement 12 93
Queries 15 34
Export table into csv file in oracle 10 43
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now