Friday 2 January 2015

How to insert values into table using Oracle stored procedure

Stored procedure is widely used by programmer to insert record into tables. IN parameter is used to insert values into tables.  

Steps
1. create simple table in which you want to insert values by procedure.
2. create procedure to insert record in table.
3. execute stored procedure multiple times to insert values into table.
4. Now use select command to view table's values.


# Step 1 : Create table.
SQL> CREATE TABLE Stu_Info
            (
                 stuid number,
                 name varchar2(10),
                 city varchar2(10),
                 amount number
            );

#Step2 : Create Procedure p_insertStuInfo to insert record into table.
SQL> CREATE OR REPLACE PROCEDURE p_InsertStuInfo
          p_stuid IN number,
             p_name  IN VARCHAR2,
             p_city  IN VARCHAR2,
             p_amount IN NUMBER)
           IS
           BEGIN
              INSERT INTO Stu_info
                 values(p_stuid,p_name,p_city,p_amount);
           END;
           /


Step 3 : Execute Procedure multiple times with different values.
Once you created procedure successfully, you can use multiple times passing simply parameters values.
SQL> EXEC p_insertStuInfo(1000,'RAM','NEW DELHI',1000);
SQL> EXEC p_insertStuInfo(1001,'SACHIN','MUMBAI',2000);
SQL> EXEC p_insertStuInfo(1002,'SMITH','NEW YORK',3000);
SQL> EXEC p_insertStuInfo(1003,'CHENG','BEIJING',4000);


Setp 4 : Check table records.
Now you can check Stu_Info table 
SQL> SELECT * from Stu_Info;

STUID
NAME
CITY
AMOUNT
1000
RAM
NEW DELHI
1000
1001
SACHIN
MUMBAI
2000
1002
SMITH
NEW YORK
3000
1003
CHENG
BEIJING
4000

No comments:

Post a Comment