本文转自: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. mexopencv问题:Invalid MEX file GLIBCXX_3.4.15 error

    参考:http://blog.sina.com.cn/s/blog_74112f030101cmxt.html root@debian-yuliyang:/opt/matlab/sys/os/glnx ...

  2. Poj 1953 World Cup Noise之解题报告

    World Cup Noise Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16369   Accepted: 8095 ...

  3. MVC框架模式技术实例(用到隐藏帧、json、仿Ajax、Dom4j、jstl、el等)

    前言: 刚刚学完了MVC,根据自己的感悟和理解写了一个小项目. 完全按照MVC模式,后面有一个MVC的理解示意图. 用MVC模式重新完成了联系人的管理系统: 用户需求: 多用户系统,提供用户注册.登录 ...

  4. Unix 基础IO

    内核通过文件描述符引用打开的文件,通常通过open函数或者create函数返回文件描述符. 基本函数: unix中基础的文件操作函数只有5个,分别是open,close,lseek,read,writ ...

  5. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇06:移动版优化指南》--本系列完结

    6.移动版优化指南 概述: 移动设备不同于目前的高端设备(Wii.Xbox 360和PS3),市场上的手机硬件是很有限的,并且所有的移动设备都是不一样的.像Adroid手机,由于品牌和出厂年限的不同, ...

  6. 检测是否安装了 .NET Framework 3.5

    此脚本是为 Internet Explorer 设计的.    其他浏览器可能在 UserAgent 字符串中不包含 .NET CLR 信息. <HTML> <HEAD> &l ...

  7. 【noip2007】树网的核

    题解: 首先我们要知道一个性质:如果有多条直径 这个核不论在哪条直径上 答案都是一样的 这样我们就可以随便找一条直径 在这条直径上枚举核的位置 并且dfs预处理maxlon[i] (i在直径上) 表示 ...

  8. TFS的使用

    1.http://www.kwstu.com/ArticleView/kwstu_201462311500744

  9. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。

    本文转载:http://blog.csdn.net/playing9c/article/details/7471918 http://blog.csdn.net/beelinkerlidejun/ar ...

  10. 一个实际的sonar代码检查的配置文件

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...