本文转自: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. 【转】C++类中对同类对象private成员访问

    私有成员变量的概念,在脑海中的现象是,以private关键字声明,是类的实现部分,不对外公开,不能在对象外部访问对象的私有成员变量. 然而,在实现拷贝构造函数和赋值符函数时,在函数里利用对象直接访问了 ...

  2. python网络编程(六)---web客户端访问

    1.获取web页面 urllib2 支持任何协议的工作---不仅仅是http,还包括FTP,Gopher. import urllib2 req=urllib2.Request('http://www ...

  3. 【安全】requests和BeautifulSoup小试牛刀

    web安全的题,为了找key随手写的程序,无处安放,姑且贴上来. # -*- coding: UTF-8 -*- __author__ = 'weimw' import requests from B ...

  4. DNN学习

    DNN(DotNetNuke)是一个免费.开源.可扩展的内容管理系统,可广泛用于商务网站.企业内网和外网网站.在线内容发布网站.DotNetNuke是微软第一次向开源说"Yes"的 ...

  5. HW7.1

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. POJ2411 - Mondriaan's Dream(状态压缩DP)

    题目大意 给定一个N*M大小的地板,要求你用1*2大小的砖块把地板铺满,问你有多少种方案? 题解 刚开始时看的是挑战程序设计竞赛上的关于铺砖块问题的讲解,研究一两天楞是没明白它代码是怎么写的,智商捉急 ...

  7. _doPostBack用法总结

    转载在以下两篇博客: http://www.cnblogs.com/yongtaiyu/archive/2011/05/13/2045746.html http://www.cnblogs.com/F ...

  8. 射频识别技术漫谈(9)——动物标签HDX【worldsing笔记】

    半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...

  9. Intent简介

    1 Intent概念 1.1 Intent的作用 指明Intent所要启动的对象 提供将要启动对象组件运行需要的数据 组件类型 启动方法 Activity startActivity(Intent i ...

  10. SQL Server 重新恢复自动编号列的序号

    1. truncate table tablename2. DBCC CHECKIDENT (tablename,reseed,1)   truncate命令不但会清除所有的数据,还会将IDENTIT ...