0

I've been struggling with this for a while.

I want to create 3 different stored procedures but i am not very sure about the syntax and how to execute them in Oracle database (version 11g)

My table contains these columns, this is the definition:

id NUMBER

amount VARCHAR2(30)

date VARCHAR2(30)

I want to create a procedure that allows me to fecth all the rows from a single table, this is what i did:

create or replace PROCEDURE GetAllRows(cursor_ OUT SYS_REFCURSOR) AS
BEGIN
OPEN cursor_ FOR
    SELECT *
      FROM My_Table  
END GetAllRows;   

Is it okay? How should i execute it?


Also , i'd like to INSERT some information to that table, so i created this another SP:

CREATE PROCEDURE insert_row(
                    id IN NUMBER,
                    amount IN VARCHAR,
                    date IN VARCHAR

                        )
IS
BEGIN

INSERT INTO My_table(id, amount, id) VALUES (id, amount, date);

END insert_row;

Is it okay? how should i execute it?

I'm kinda lost with Oracle databases.

1
  • 1
    Regarding the first example, we can use views for stored queries. You can have procedures that return cursors but you'll need to write code to handle the cursor until Oracle 12.1 (see stackoverflow.com/a/40360471/230471). It's not worth using a procedure for this kind of thing unless the query is complex and dynamic. Commented Feb 20, 2021 at 10:01

2 Answers 2

2

Please try with the below code to get stored procedure output.

variable rc refcursor;
exec GetAllRows( :rc );
print rc;
13
  • Hey pal, thanks for replying I do not understand. Are my SPs well written?
    – JustToKnow
    Commented Feb 19, 2021 at 17:41
  • Yes, Both SP are ok Commented Feb 19, 2021 at 17:44
  • Could you please tell me how to execute the SP within the insert statement?
    – JustToKnow
    Commented Feb 19, 2021 at 17:45
  • 1
    @Belayer - you beat me to it. I'd also add that starting or ending a name with underscore ('cursor_') is probably not a great idea either. Technically it will work, but I'd be concerned that some human maintaining the code would mis-read/mis-type it. Save the underscore for joining two words.
    – EdStevens
    Commented Feb 19, 2021 at 19:44
  • 1
    Nit picking. Naming format consistency. Never use CamelCase (oracle will spit back CAMELCASE) making it hard to read. And purely personal preference I like specific routine names so maybe "get_mytable_rows" and "insert_mytable_row". And I write everything in lower case (many will disagree with that). The important thing is not how you write (within reason). But 3 very important concepts: 1, Names are important choose them well and with thought. 2. Code must be easy to read, if it is not it cannot be understand. 3. Choose your style, and then ALWAYS be consistent with it.
    – Belayer
    Commented Feb 19, 2021 at 20:26
1

It's hard to talk about 'style' or 'well written' without being able to give an example, and the framework of SO only allows that in an 'answer' not a 'comment'. So I'm going to stretch the meaning of 'answer' and show you how I would have coded it:

CREATE PROCEDURE insert_row(p_descriptive_id IN NUMBER,
                            p_descriptive_amount IN VARCHAR,
                            p_descriptive_date IN VARCHAR
                            )
IS
BEGIN

   INSERT INTO My_table(descriptive_id, 
                        descriptive_amount, 
                        descriptive_date) 
                 VALUES (p_descriptive_id, 
                         p_descriptive_amount, 
                         p__descriptive_date);

END insert_row;

Comments:

  1. all field and column names are 'descriptive' - adjective_noun format
  2. all parms begin with 'p_', to differentiate bet. parms and columns. Some people like a prefix that indicates if it is input or output parm (i_descriptive_input, o_descriptive_output.
  3. In your INSERT line the column names (and values) in a column, not strung out on a single line. Basic rule, comma-delimited lists should line-break on the comma so the list is in a column. Easier to read and take in what's there. Especially on an INSERT when you need to make sure your table columns and values match.
2
  • Thank you very much for replying, it means a look, pal. Can i ask you something? What would you do to SELECT all tuples or some columns in your SP? Would it be similar to the one i created?. is there exist a different way to create it in Oracle DATABASE (11g version)?
    – JustToKnow
    Commented Feb 20, 2021 at 0:55
  • Depends on what you are going to do with them. One thing for sure, except for ad hoc queries, you should not simply 'select * from my_table'. Such a statement could break your code if the table structure were to change. Presumably, in production code you know which columns you need -- so list them: 'select emp_id, first_name, last_name from employees'.
    – EdStevens
    Commented Feb 20, 2021 at 13:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.