Solved

Oracle PL/SQL insert, commit statement DECLARE error

Posted on 2012-03-14
19
2,330 Views
Last Modified: 2012-04-13
Hello - I'm trying to write a simple PL/SQL statement to speed up inserting rows into a table.  I have a 5bil row table.  Below is my code and I keep getting DECLARE errors.  Any help appreciated!  Or if there is a better/faster way, please let me know.  thanks!

SQL> create or replace procedure AC_NOTE
  2  as
  3  DECLARE
  4  Col1 ACC_LOG_DTL_IX_OLD.ACCESS_INSTANT%TYPE;
  5  Col2 ACC_LOG_DTL_IX_OLD.PROCESS_ID%TYPE;
  6  Col3 ACC_LOG_DTL_IX_OLD.DATA_MNEMONIC_ID%TYPE;
  7  Col4 ACC_LOG_DTL_IX_OLD.STRING_VALUE%TYPE;
  8  Col5 ACC_LOG_DTL_IX_OLD.INTEGER_VALUE%TYPE;
  9  INSERT_COUNT NUMBER := 1;
 10  BEGIN
 11  CURSOR c1 IS select * from  ACC_LOG_DTL_IX_OLD where  DATA_MNEMONIC_ID = 'NOTE';
 12  FOR r1 IN c1 LOOP
 13  INSERT
 14  INTO ACC_LOG_DTL_IX
 15  (ACCESS_INSTANT,
 16       PROCESS_ID,
 17       DATA_MNEMONIC_ID,
 18       STRING_VALUE,
 19       INTEGER_VALUE)
 20  VALUES
 21  (r1.Col1, r1.Col2,r1.Col3,r1.Col4,r1.Col5);
 22  IF INSERT_COUNT = 1000 THEN
 23  COMMIT;
 24  INSERT_COUNT := 1;
 25  ELSE
 26  INSERT_COUNT := INSERT_COUNT + 1;
 27  END IF;
 28  END LOOP;
 29  COMMIT;
 30  END
 31  ;
 32  /

Warning: Procedure created with compilation errors.

Elapsed: 00:00:00.12


 show errors
Errors for PROCEDURE AC_NOTE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
3/1      PLS-00103: Encountered the symbol "DECLARE" when expecting one of
         the following:
         begin function pragma procedure subtype type <an identifier>
         <a double-quoted delimited-identifier> current cursor delete
         exists prior external language
         The symbol "begin" was substituted for "DECLARE" to continue.

11/8     PLS-00103: Encountered the symbol "C1" when expecting one of the
         following:
         := . ( @ % ;
0
Comment
Question by:diannagibbs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
19 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 37722118
Just remove the declare keyword. This is only used when creating an anonymous PL/SQL block not a stored procedure.
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 37722127
You can't use the word "declare" inside a named PL\SQL procedure.  You only need that keyword if you want to run an anonymouse (or unnamed) PL\SQL block of code.
0
 
LVL 32

Expert Comment

by:awking00
ID: 37722130
Also cursor c1 needs to come after the begin keyword.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:awking00
ID: 37722132
I meant to say before the begin keyword.
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 37722148
Also, if you really have a 5 billion row table (that's large!), are you sure that you want to use a cursor loop to copy individual records one-at-a-time to a new table?  That process may keep your server busy for a LONG time!
0
 
LVL 32

Expert Comment

by:awking00
ID: 37722150
SQL> create or replace procedure AC_NOTE
as
 Col1 ACC_LOG_DTL_IX_OLD.ACCESS_INSTANT%TYPE;
 Col2 ACC_LOG_DTL_IX_OLD.PROCESS_ID%TYPE;
 Col3 ACC_LOG_DTL_IX_OLD.DATA_MNEMONIC_ID%TYPE;
 Col4 ACC_LOG_DTL_IX_OLD.STRING_VALUE%TYPE;
 Col5 ACC_LOG_DTL_IX_OLD.INTEGER_VALUE%TYPE;
 INSERT_COUNT NUMBER := 1;
 CURSOR c1 IS select * from  ACC_LOG_DTL_IX_OLD where  DATA_MNEMONIC_ID =  'NOTE';
 BEGIN
 FOR r1 IN c1 LOOP
 INSERT
 INTO ACC_LOG_DTL_IX
 (ACCESS_INSTANT,
      PROCESS_ID,
      DATA_MNEMONIC_ID,
      STRING_VALUE,
      INTEGER_VALUE)
 VALUES
 (r1.Col1, r1.Col2,r1.Col3,r1.Col4,r1.Col5);
 IF INSERT_COUNT = 1000 THEN
 COMMIT;
 INSERT_COUNT := 1;
 ELSE
 INSERT_COUNT := INSERT_COUNT + 1;
 END IF;
 END LOOP;
 COMMIT;
 END
 ;
 /
0
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 500 total points
ID: 37722169
How about this (likely *MUCH* faster) alternative:

alter table ACC_LOG_DTL_IX nologging;

-- If this table has indexes, you should do an
-- "alter index [index_name] nologging;" command
-- for each of them as well.

insert /*+APPEND */ into ACC_LOG_DTL_IX
(ACCESS_INSTANT,
      PROCESS_ID,
      DATA_MNEMONIC_ID,
      STRING_VALUE,
      INTEGER_VALUE)
select * from  ACC_LOG_DTL_IX_OLD
where  DATA_MNEMONIC_ID = 'NOTE';
0
 

Author Comment

by:diannagibbs
ID: 37722184
Any ideas on a faster way of doing this?  I've been trying to use SQLPlus but it's taking forever, not to mention running out of UNDO, etc.  I'm open to any ideas?  Thanks!
0
 

Author Comment

by:diannagibbs
ID: 37722211
Took out DECLARE...now this error?

LINE/COL ERROR
-------- -----------------------------------------------------------------
12/3     PL/SQL: SQL Statement ignored
20/39    PL/SQL: ORA-00984: column not allowed here
20/39    PLS-00302: component 'COL5' must be declared
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 37722233
The example I just gave you is for SQL*Plus, since it includes the DDL command "alter table ..." that PL\SQL cannot handle directly.  And, it avoids the undo and redo overhead through the combination of the "nologging" command and "insert /*+APPEND */ ..." syntax.

If you've never used the "nologging" command before, don't worry that this will leave your data vulnerable to a disk failure.  "Nologging" only applies to a very tiny sub-set of SQL activites (inserts) and then only if the insert command includes the "APPEND" hint.  So, all "normal" inserts, updates and deletes will be logged anyway.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37722246
if you can't do it in a single statement as markgeer showed above, then use bulk operations.
Definitely don't do it row-by-row.

Assuming it finishes one row in 1 millisecond (that's unrealistically optimistic) it'll take you 5 million seconds to finish, or about 57 days.
0
 

Author Comment

by:diannagibbs
ID: 37722252
I'm an Oracle DBA and have learned to never use nologging.  Recovery can be an issue.   As for your statement, that's what I've been doing in SqlPlus and just taking too long.  I always understood a procedure/cursor would be faster.  You're saying this is not true?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37722263
>>> understood a procedure/cursor would be faster.  You're saying this is not true?

"faster" than what?  pulling the data out row by row to a java/c#/vb.net routine that then inserts the rows one at a time?  yes, the pl/sql routine will be faster.  Don't laugh, I've seen people do that.

but, a single sql statement will always (provided you have the resources to let it complete) be faster and overall more efficient than procedural code, especially with looping.
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 37722267
"I always understood a procedure/cursor would be faster."

Definitely *NOT* true!

A simple SQL statement is almost always faster than a PL\SQL procedure that includes a cursor!  If you use "bulk collect' in PL\SQL, then the perormance of PL\SQL can approach that of simple SQL.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37722275
not just bulk collect on the select, but also FORALL  (yes, that's one word) on the insert
0
 

Author Comment

by:diannagibbs
ID: 37722284
Great thanks for news.  I have been running the basic statement so will continue with that route.  Will also look into your "no logging" but have heard too many horror stories of not being able to recover - months later - with using it.   One more question, I've also been using "set autocommit 2500" but doesn't appear to be commiting every 2500 rows.  Am I msising somethng?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37722301
>>> too many horror stories of not being able to recover

after you complete a nologging operation, take a backup.  
That's really all there is to keeping it recoverable.
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 37722302
If you use "nologging" and are concerned about recovery, you simply need to do a backup of the affected tablespace(s) after the "nologging" operation.

The "set autocommit" command is a SQL*Plus command, not a PL\SQL command.  So, that has no affect inside a PL\SQL procedure.
0
 

Author Comment

by:diannagibbs
ID: 37722314
Yes, I understand the set autocommit...that's what I have been using with sqlplus in my original statements before I tried PL/SQL.  We do backups nightly, so should be good.  Thanks - I'll test timings.  Appreciate it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

617 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