0% found this document useful (0 votes)
238 views

Oracle Stored Procedure I

The document provides examples of using stored procedures in Oracle to perform CRUD (create, read, update, delete) operations on a database table. It includes examples of inserting, updating, deleting records with stored procedures. It also includes an example of a stored procedure that uses a SELECT INTO statement to retrieve records and assign them to output parameters. The document discusses how to call the stored procedures from PL/SQL and provides the full code examples for the stored procedures and the SQL scripts to create the database table. It also includes examples of creating functions in Oracle to encapsulate reusable logic and examples calling functions within SQL statements.

Uploaded by

Vic Vickyliz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

Oracle Stored Procedure I

The document provides examples of using stored procedures in Oracle to perform CRUD (create, read, update, delete) operations on a database table. It includes examples of inserting, updating, deleting records with stored procedures. It also includes an example of a stored procedure that uses a SELECT INTO statement to retrieve records and assign them to output parameters. The document discusses how to call the stored procedures from PL/SQL and provides the full code examples for the stored procedures and the SQL scripts to create the database table. It also includes examples of creating functions in Oracle to encapsulate reusable logic and examples calling functions within SQL statements.

Uploaded by

Vic Vickyliz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Oracle Stored Procedure INSERT example

1. Table SQL Script


DBUSER table creation script.
CREATE TABLE DBUSER (
USER_ID

NUMBER (5)

NOT NULL,

USERNAME

VARCHAR2 (20) NOT NULL,

CREATED_BY

VARCHAR2 (20) NOT NULL,

CREATED_DATE DATE

NOT NULL,

PRIMARY KEY ( USER_ID )


)
2. Stored Procedure
A stored procedure, accept 4 IN parameters and insert it into table
DBUSER.
CREATE OR REPLACE PROCEDURE insertDBUSER(
p_userid IN DBUSER.USER_ID%TYPE,
p_username IN DBUSER.USERNAME%TYPE,
p_createdby IN DBUSER.CREATED_BY%TYPE,
p_date IN DBUSER.CREATED_DATE%TYPE)
IS
BEGIN

INSERT INTO DBUSER ("USER_ID", "USERNAME", "CREATED_BY",


"CREATED_DATE")
VALUES (p_userid, p_username,p_createdby, p_date);

COMMIT;

END;
/
3. Calls from PL/SQL
Call from PL/SQL like this :
BEGIN
insertDBUSER(1001,'mkyong','system',SYSDATE);
END;

Oracle Stored Procedure UPDATE example


1. Table SQL Script DBUSER table creation script.
CREATE TABLE DBUSER (
USER_ID

NUMBER (5)

NOT NULL,

USERNAME

VARCHAR2 (20) NOT NULL,

CREATED_BY

VARCHAR2 (20) NOT NULL,

CREATED_DATE DATE

NOT NULL,

PRIMARY KEY ( USER_ID )


)

2. Stored Procedure
A stored procedure, accept 2 IN parameters and update the username field based
on the provided userId.
CREATE OR REPLACE PROCEDURE updateDBUSER(
p_userid IN DBUSER.USER_ID%TYPE,
p_username IN DBUSER.USERNAME%TYPE)
IS
BEGIN

UPDATE DBUSER SET USERNAME = p_username where USER_ID = p_userid;

COMMIT;

END;
/

3. Calls from PL/SQL


Call from PL/SQL like this :
BEGIN

updateDBUSER(1001,'new_mkyong');
END;

Oracle Stored Procedure DELETE example


1. Table SQL Script
DBUSER table creation script.
CREATE TABLE DBUSER (
USER_ID

NUMBER (5)

NOT NULL,

USERNAME

VARCHAR2 (20) NOT NULL,

CREATED_BY

VARCHAR2 (20) NOT NULL,

CREATED_DATE DATE

NOT NULL,

PRIMARY KEY ( USER_ID )


)

2. Stored Procedure
A stored procedure, delete the record base on the provided userId.
CREATE OR REPLACE PROCEDURE deleteDBUSER(p_userid IN DBUSER.USER_ID%TYPE)
IS
BEGIN

DELETE DBUSER where USER_ID = p_userid;

COMMIT;

END;
/

3. Calls from PL/SQL


Call from PL/SQL like this :
BEGIN
deleteDBUSER(1001);
END;

Result
Record of userid=1001 is deleted via deleteDBUSER store procedure.

Oracle Stored Procedure SELECT INTO example


1. Table SQL Script

DBUSER table creation script.


CREATE TABLE DBUSER (
USER_ID

NUMBER (5)

NOT NULL,

USERNAME

VARCHAR2 (20) NOT NULL,

CREATED_BY

VARCHAR2 (20) NOT NULL,

CREATED_DATE DATE
PRIMARY KEY ( USER_ID )
)

NOT NULL,

2. Stored Procedure
A stored procedure, uses SELECT INTO mechanism to assign the matched values to
OUT parameters.
CREATE OR REPLACE PROCEDURE getDBUSERByUserId(
p_userid IN DBUSER.USER_ID%TYPE,
o_username OUT DBUSER.USERNAME%TYPE,
o_createdby OUT DBUSER.CREATED_BY%TYPE,
o_date OUT DBUSER.CREATED_DATE%TYPE)
IS
BEGIN

SELECT USERNAME , CREATED_BY, CREATED_DATE


INTO o_username, o_createdby, o_date
from DBUSER WHERE USER_ID = p_userid;

END;
/

3. Calls from PL/SQL


Call from PL/SQL like this :
DECLARE
o_username DBUSER.USERNAME%TYPE;
o_createdby DBUSER.CREATED_BY%TYPE;

o_date DBUSER.CREATED_DATE%TYPE;
BEGIN

getDBUSERByUserId(1001,o_username,o_createdby,o_date);

DBMS_OUTPUT.PUT_LINE('username : ' || o_username);


DBMS_OUTPUT.PUT_LINE('createdby : ' || o_createdby);
DBMS_OUTPUT.PUT_LINE('createddate : ' || o_date);

END;
/

Result
Record with userId=1001 is displayed via getDBUSERByUserId store procedure.

CREATE FUNCTION - Examples


It is suggested to use the CREATE OR REPLACE command to create functions. This allows replacement
of existing triggers. To ensure you don't replace a FUNCTION that is already in your schema, query the
USER_TRIGGERS view before issuing the CREATE OR REPLACE FUNCTION command.
Here is an example CREATE FUNCTION command:
SQL> CREATE OR REPLACE FUNCTION minimum(v1 number, v2 number) RETURN number IS
1: BEGIN
2:
IF v1 < v2 THEN
3:
RETURN v1;
4:
ELSE
5:
RETURN v2;
6:
END IF;
7: END;
The following statement creates the function GET_BAL.
CREATE FUNCTION get_bal(acc_no IN NUMBER)

RETURN NUMBER
IS acc_bal NUMBER(11,2);
BEGIN
SELECT balance
INTO acc_bal
FROM accounts
WHERE account_id = acc_no;
RETURN(acc_bal);
END;
The GET_BAL function returns the balance of a specified account.
When you call the function, you must specify the argument ACC_NO, the number of the account whose
balance is sought. The datatype of ACC_NO is NUMBER.
The function returns the account balance. The RETURN clause of the CREATE FUNCTION statement
specifies the datatype of the return value to be NUMBER.
The function uses a SELECT statement to select the BALANCE column from the row identified by the
argument ACC_NO in the ACCOUNTS table. The function uses a RETURN statement to return this value
to the environment in which the function is called.
The function created above can be used in a SQL statement. For example:
SELECT get_bal(100) FROM DUAL;
The following statement creates PL/SQL standalone function GET_VAL that registers the C routine
C_GET_VAL as an external function. (The parameters have been omitted from this example.)
CREATE FUNCTION get_val
( x_val IN NUMBER,
y_val IN NUMBER,
image IN LONG RAW )
RETURN BINARY_INTEGER AS LANGUAGE C
NAME "c_get_val"
LIBRARY c_utils
PARAMETERS (...);

http://www.compiletimeerror.com/2013/12/content-provider-inandroid.html
http://developer.android.com/guide/topics/providers/content-providercreating.html
https://newcircle.com/s/post/1375/android_content_provider_tutorial
http://code.tutsplus.com/tutorials/android-fundamentals-working-withcontent-providers--mobile-5549

You might also like