本文转自:http://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/

List of quick examples to create stored procedures (IN, OUT, IN OUT and Cursor parameter) in Oracle database. PL/SQL code is self-explanatory.

1. Hello World

A stored procedure to print out a “Hello World” via DBMS_OUTPUT.

CREATE OR REPLACE PROCEDURE procPrintHelloWorld
IS
BEGIN DBMS_OUTPUT.PUT_LINE('Hello World!'); END;
/

Run it

EXEC procPrintHelloWorld;

Output

Hello World!
2. Hello World + IN Parameter

A stored procedure to accept a single parameter and print out the “Hello World IN parameter” + parameter value via DBMS_OUTPUT.

CREATE OR REPLACE PROCEDURE procOneINParameter(param1 IN VARCHAR2)
IS
BEGIN DBMS_OUTPUT.PUT_LINE('Hello World IN parameter ' || param1); END;
/

Run it

EXEC procOneINParameter('mkyong');

Output

Hello World IN parameter mkyong

3. Hello World + OUT Parameter

A stored procedure to output/assign the “Hello World OUT parameter” value to OUT parameter.

CREATE OR REPLACE PROCEDURE procOneOUTParameter(outParam1 OUT VARCHAR2)
IS
BEGIN outParam1 := 'Hello World OUT parameter'; END;
/

Run it

DECLARE
outParam1 VARCHAR2();
BEGIN
procOneOUTParameter(outParam1);
DBMS_OUTPUT.PUT_LINE(outParam1);
END;
/

Output

Hello World OUT parameter

4. Hello World + INOUT Parameter

A stored procedure to accept a INOUT parameter (genericParam), construct the output message and assign back to the same parameter name(genericParam) again.

CREATE OR REPLACE PROCEDURE procOneINOUTParameter(genericParam IN OUT VARCHAR2)
IS
BEGIN genericParam := 'Hello World INOUT parameter ' || genericParam; END;
/

Run it

DECLARE
genericParam VARCHAR2() := 'mkyong';
BEGIN
procOneINOUTParameter(genericParam);
DBMS_OUTPUT.PUT_LINE(genericParam);
END;
/

Output

Hello World INOUT parameter mkyong

5. Hello World + Cursor

A stored procedure, return a ref cursor and accept a IN parameter.

CREATE OR REPLACE PROCEDURE procCursorExample(
cursorParam OUT SYS_REFCURSOR, userNameParam IN VARCHAR2)
IS
BEGIN OPEN cursorParam FOR
SELECT * FROM DBUSER WHERE USERNAME = userNameParam; END;
/

Run it

DECLARE
dbUserCursor SYS_REFCURSOR;
dbUserTable DBUSER%ROWTYPE;
BEGIN procCursorExample(dbUserCursor,'mkyong'); LOOP FETCH dbUserCursor INTO dbUserTable; EXIT WHEN dbUserCursor%NOTFOUND;
dbms_output.put_line(dbUserTable.user_id); END LOOP; CLOSE dbUserCursor; END;
/

Output

List OF the user_id which matched username='mkyong'

Reference

  1. http://www.oradev.com/ref_cursor.jsp
  2. http://psoug.org/reference/procedures.html
  3. http://www.devshed.com/c/a/Oracle/Working-with-REF-CURSOR-in-PL-SQL/
  4. http://www.codeproject.com/KB/database/Oracle_RefCursor_ADO_C__.aspx

[转]Oracle Stored Procedures Hello World Examples的更多相关文章

  1. Spring, Hibernate and Oracle Stored Procedures

    一篇英文博文,写的是利用hibernate处理存储过程中的游标等等: Motivation: While there are a few resources available online for ...

  2. [转]How to: Execute Oracle Stored Procedures Returning RefCursors

    本文转自:http://www.telerik.com/help/openaccess-orm/openaccess-tasks-oracle-execute-sp-result-set.html I ...

  3. MySQL Error Handling in Stored Procedures 2

    Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...

  4. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

  5. Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python

    f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...

  6. [MySQL] Stored Procedures 【转载】

    Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...

  7. Good Practices to Write Stored Procedures in SQL Server

    Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored ...

  8. An Introduction to Stored Procedures in MySQL 5

    https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843 MySQL ...

  9. Cursors in MySQL Stored Procedures

    https://www.sitepoint.com/cursors-mysql-stored-procedures/ After my previous article on Stored Proce ...

随机推荐

  1. MSP430的比较器

    这两天研究了一下430的比较器,开始的时候,没有看懂是怎么一回事,在网站看这方面的博客,好像懂了,但是一到编程,就变得无从下手,但是,皇天不负有心人,笔者还是把他弄懂了 其实这里就是看懂一幅图,两个寄 ...

  2. 多线程与网络之JSON和XML数据的解析

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. 《UNIX环境高级编程 第2版》读书笔记

    CH1-2:基础知识.标准化 1 文件和目录 文件名:不能含/(分隔路径)和null(终止路径),255字符. 目录处理:opendir() readdir() closedir() 更改工作目录:c ...

  4. JDK1.5新特性(六)……Generics

    概述 Generics - This long-awaited enhancement to the type system allows a type or method to operate on ...

  5. Max Sub-matrix

    Max Sub-matrix 教练找的题目,目前样列过了 题意:找子矩阵的最大周长 思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的 ...

  6. 6.2 CUDA streams

    stream是什么 nivdia给出的解释是:A sequence of operations that execute in issue-order on the GPU.  可以理解成在GPU上执 ...

  7. varchar

    mysql varchar(50) 不管中文 还是英文 都是存50个的 MySQL5的文档,其中对varchar字段类型这样描述:varchar(m) 变长字符串.M 表示最大列长度.M的范围是0到6 ...

  8. 深度学习-使用cuda加速卷积神经网络-手写数字识别准确率99.7%

    源码和运行结果 cuda:https://github.com/zhxfl/CUDA-CNN C语言版本参考自:http://eric-yuan.me/ 针对著名手写数字识别的库mnist,准确率是9 ...

  9. OpenCV中的矩阵乘法运算

    转载:http://blog.csdn.net/tangwei2014 OpenCV中矩阵乘法运算 1. Mat*Mat: 第一个矩阵的列数必须等于第二个矩阵的行数. [0, 1, 2, 3;     ...

  10. Android 开发Project中各个目录和文件的介绍

    如上图标号: 存放java文件的文件夹“src”: 由aapt工具根据应用中的资源文件自动生成的R.java文件,以及buildConfiger.java文件,这两个文件最好不要去修改: 存放各种资源 ...