You could use either a PROCEDURE or a FUNCTION depending upon your ultimate requirements.
A procedure would get the job done but you'd have to then query the MAYA table to see how many records you inserted.
i.e.
CREATE OR REPLACE
PROCEDURE maya_insert
IS
BEGIN
-- Truncate the maya table
EXECUTE IMMEDIATE 'TRUNCATE TABLE MY_SCHEME.MAYA';
--
-- Insert records into maya
INSERT INTO MY_SCHEME.MAYA
(
ID_TEST,
IQ,
DATE_,
COMMENT1
)
SELECT ID_TEST,
IQ,
DATE_,
COMMENT1
FROM MY_SCHEME.STAGE_MAYA
WHERE STAGE_MAYA.ID_TEST = (SELECT MAX(ID_TEST)
FROM MY_SCHEME.STAGE_MAYA);
EXCEPTION
WHEN others
THEN
DBMS_OUTPUT.put_line('MAYA_INSERT error: '||sqlerrm);
END maya_insert;
You may want to put a COMMIT; in there too unless you are committing the transaction outside of this procedure, if you do add the COMMIT; then also add a ROLLBACK; in the exception section.
Be aware though that the TRUNCATE statement cannot be rolled back as it's DDL. If you need the ability to roll it back then you'll have to use the slower DELETE command which is DML.
If you needed to know how many records were inserted into MAYA then use a function:
i.e.
CREATE OR REPLACE
FUNCTION maya_insert
RETURN NUMBER
IS
BEGIN
-- Truncate the maya table
EXECUTE IMMEDIATE 'TRUNCATE TABLE MY_SCHEME.MAYA';
--
-- Insert records into maya
INSERT INTO MY_SCHEME.MAYA
(
ID_TEST,
IQ,
DATE_,
COMMENT1
)
SELECT ID_TEST,
IQ,
DATE_,
COMMENT1
FROM MY_SCHEME.STAGE_MAYA
WHERE STAGE_MAYA.ID_TEST = (SELECT MAX(ID_TEST)
FROM MY_SCHEME.STAGE_MAYA);
-- Return the number of records inserted
RETURN SQL%ROWCOUNT;
EXCEPTION
WHEN others
THEN
DBMS_OUTPUT.put_line('MAYA_INSERT error: '||sqlerrm);
RETURN -1;
END maya_insert;
Here you will get the count of records inserted and if there was an error then you'll get -1 returned. See above for my comments on COMMIT; and ROLLBACK; being added if you need them.
As regards a package. Packages are used to group logically related functions procedures and other processing together in the database. As you only have one procedure or function there is no need to wrap it in a package at this stage.
See: http://docs.oracle.com/cd/B10501_01/appdev.920/a96624/09_packs.htm#362
Hope it helps...
CREATE OR REPLACE PROCEDURE MAY12_PROC IS BEGIN EXECUTE IMMEDIATE 'before your TRUNCATE statement. Put'between theMAYAand;at the end of theTRUNCATEstatement. Add; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLCODE || ' ' || SQLERRM); END MAY12_PROC;after your last line.