Reputation: 2520
Could anybody help me to convert the following sql code into procedure? I've read some www sources and made a conclusion (may be) that it should be package?
TRUNCATE TABLE MY_SCHEME.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)
Thanks a lot!
Upvotes: 0
Views: 1696
Reputation: 17578
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...
Upvotes: 2
Reputation: 100
A package can contain multiple procedures, functions, variables, and definitions. You issue grants on the package not on the functions it contains. A major difference is that when the code of a function in a package needs to change, you can replace the package body (create or replace syntax) and this will not cause invalidation of dependant code. A change to a function outside a package will always cause invalidation of dependant code.
In your case you'll need a procedure
Upvotes: 0