Reputation: 285
I am NEW to PL/SQL so please don't mark me down if this seems too beginner-like of a question. I have spent a couple hours working on this and am now seeking help. Additionally, please bear in mind that this code is incomplete. I'm seeking help to complete it. So I am trying to create a simple procedure for my test table called KEYBOARD_LEARNING. I used to have to manually use this code:
INSERT INTO keyboard_learning (emplid, wpm, date_completed,exercise,attempt,score_lvl)
VALUES ('000000000','37.66','04-JUL-2012','Keyhero.com','28','95.87% accuracy')
..for every time I wanted to log a new score to KEYBOARD_LEARNING. I figured I could make a procedure to handle this for me but I need it to not be static because the values I input when I call this procedure are always changing. Any thoughts on how I can make the code I have work with this optimal funcionality? By the way, the code below is not executing, which I'm sure an experienced Oracle user will be able to figure out why right away.
Thank you
CREATE OR REPLACE PROCEDURE INSERT_WPM_SCORE
(
P_EMPLID VARCHAR2
,P_WPM NUMBER
,P_DATE_COMPLETED SYSDATE
,P_EXERCISE VARCHAR2
,P_ATTEMPT VARCHAR2
,P_SCORE_LVL VARCHAR2
) AS
/*
Original Author:
Created Date: 2-Aug-2012
Purpose: For inputting latest WPM score from typing practice
*/
/*variables*/
L_EMPLID VARCHAR2(4000);
L_WPM NUMBER;
L_DATE_COMPLETED SYSDATE;
L_EXERCISE VARCHAR2(4000);
L_ATTEMPT VARCHAR2(4000);
L_SCORE_LVL VARCHAR2(4000);
L_PREVENT_NULL_INPUT EXCEPTION;
PRAGMA EXCEPTION_INIT(L_PREVENT_NULL_INPUT, -44002); --GET CORRECT ERROR # BY TESTING WITHOUT EXCEPTION
VALUES VARCHAR2(4000); [b]<-- getting an error here[/b]
/*Procedure 'W' is a wrapper for DBMS output*/
PROCEDURE W(STR VARCHAR2) IS
L_STRING VARCHAR2(4000);
BEGIN
/*Outputting string parameter passed into 'W' procedure*/
L_STRING := STR;
DBMS_OUTPUT.PUT_LINE(STR);
END;
BEGIN
VALUES := (L_EMPLID, L_WPM, L_DATE_COMPLETED, L_EXERCISE, L_ATTEMPT,L_SCORE_LVL);
SELECT INTO (SELECT *
FROM KEYBOARD_LEARNING A
ORDER BY A.EXERCISE
,TO_NUMBER(ATTEMPT))
-- DBMS_OUTPUT.PUT_LINE(RESULTS);
EXCEPTION
/* */
WHEN L_PREVENT_NULL_INPUT THEN
NULL;
/*this exception catches all other exceptions*/
WHEN OTHERS THEN
W('ERROR: ' || SQLERRM);
END;
Upvotes: 2
Views: 688
Reputation: 231651
First, let's implement the simplest possible thing that could work
CREATE OR REPLACE PROCEDURE INSERT_WPM_SCORE
(
P_EMPLID VARCHAR2
,P_WPM NUMBER
,P_DATE_COMPLETED DATE DEFAULT SYSDATE
,P_EXERCISE VARCHAR2
,P_ATTEMPT VARCHAR2
,P_SCORE_LVL VARCHAR2
) AS
BEGIN
INSERT INTO keyboard_learning (emplid, wpm, date_completed,exercise,attempt,score_lvl)
VALUES( p_emplid, p_wpm, p_date_completed, p_exercise, p_attempt, p_score_lvl );
END;
Assuming that works, then you can work on adding additional functionality to the procedure. Unfortunately, it's not obvious to me what additional functionality you want to implement.
NULL
input, for example, but I'm not sure what that is trying to accomplish. Are you trying to validate the inputs to the procedure? Are you trying to catch an exception that is thrown when you do the INSERT
and violate a NOT NULL
constraint? Something else? SELECT
statement in your original post is intended to do. Perhaps you are trying to override the p_attempt
that is passed in to the procedure?Additionally, as a general rule, catching exceptions that you cannot handle is a mistake and a WHEN OTHERS
that is not followed by a RAISE
is almost always a mistake. If you catch the exception and write the SQLERRM
to DBMS_OUTPUT
, you lose all the useful stack trace information that the exception provides, you lose the line number of the piece of code that generated the error, you are reliant on the client application to actually enable the DBMS_OUTPUT
buffer and to read from it (most client applications do not), and you are preventing the caller of your procedure from determining that the call failed. You would be better served by eliminating your exception handlers entirely.
Upvotes: 4