存儲過程是實際位於 Oracle 中的程序
sayHello (name IN VARCHAR
調用此過程時
PROCEDURE edit_entry(
status_out OUT NUMBER
status_msg_out OUT VARCHAR
id_inout IN OUT INTEGER
title_in IN VARCHAR
text_out OUT CLOB
categories_in IN list_of_numbers
);
該過程名為 edit_entry
status_out OUT NUMBER
內部名稱為 status_out
id_inout IN OUT INTEGER
它的模式為 IN OUT
categories_in IN list_of_numbers
此處的類型是由用戶定義的(稍後將對該類型進行詳細介紹)
IN – 該模式的參數由調用方提供
OUT – 參數可以由過程分配值並返回至調用方
IN OUT – 參數可以在兩個
參數項是必選項
<?php
// etc
$sql =
然後
CREATE OR REPLACE PROCEDURE
sayHello (name IN VARCHAR
AS
BEGIN
greeting :=
END;
/
注意
$ sqlplus username@SID
然後
SQL> START /home/username/SAYHELLO
以下 PHP 腳本調用該過程
<?php
$conn = oci_connect(
$sql =
$stmt = oci_parse($conn
// Bind the input parameter
oci_bind_by_name($stmt
// Bind the output parameter
oci_bind_by_name($stmt
// Assign a value to the input
$name =
oci_execute($stmt);
// $message is now populated with the output value
print
?>
Blog 示例程序包
CREATE OR REPLACE PACKAGE blog AS
TYPE cursorType IS REF CURSOR RETURN blogs%ROWTYPE;
/*
Fetch the latest num_entries_in from the blogs table
entries_cursor_out with the result
*/
PROCEDURE latest(
num_entries_in IN NUMBER
entries_cursor_out OUT cursorType
);
/*
Edit a blog entry
attempts to UPDATE the existing blog entry
containing a description
categories_in is a collection where list_of_numbers is described by
TYPE list_of_numbers AS VARRAY(
*/
PROCEDURE edit_entry(
status_out OUT NUMBER
status_msg_out OUT VARCHAR
id_inout IN OUT INTEGER
title_in IN VARCHAR
text_out OUT CLOB
categories_in IN list_of_numbers
);
END blog;
/
該程序包提供了兩個過程
由以下語句定義
CREATE OR REPLACE TYPE list_of_numbers AS VARRAY(
下面顯示了該程序包的主體
CREATE OR REPLACE PACKAGE BODY blog AS
/*
PROCEDURE latest(
num_entries_in IN NUMBER
entries_cursor_out OUT cursorType
) AS
BEGIN
OPEN entries_cursor_out FOR
SELECT * FROM blogs WHERE rownum < num_entries_in
ORDER BY date_published DESC;
END latest;
/*
PROCEDURE edit_entry(
status_out OUT NUMBER
status_msg_out OUT VARCHAR
id_inout IN OUT INTEGER
title_in IN VARCHAR
text_out OUT CLOB
categories_in IN list_of_numbers
AS
ENTRY_NOT_FOUND EXCEPTION;
entry_found INTEGER :=
BEGIN
/* Default status to success */
status_out :=
/* If id_inout has a value then attempt to UPDATE */
IF id_inout IS NOT NULL THEN
/* Check the id exists
SELECT COUNT(*) INTO entry_found
FROM blogs b WHERE b
IF entry_found !=
/* Update the blogs table returning the CLOB field */
UPDATE blogs b SET b
WHERE b
/* Remove any existing relationships to categories
DELETE FROM blogs_to_categories WHERE blog_id = id_inout;
status_msg_out :=
/* id_inout was null so INSERT new record */
ELSE
INSERT INTO blogs b ( b
VALUES ( blog_id_seq
RETURNING b
status_msg_out :=
END IF;
/* Now handle assignment to categories
Loop over the categories_in collection
inserting the new category assignments */
FOR i IN
LOOP
INSERT INTO blogs_to_categories (blog_id
VALUES (id_inout
END LOOP;
status_msg_out := status_msg_out ||
|| unt ||
EXCEPTION
/* Catch the exception when id_inout not found */
WHEN ENTRY_NOT_FOUND THEN
status_out :=
status_msg_out :=
|| id_inout;
/* Catch any other exceptions raised by Oracle */
WHEN OTHERS THEN
status_out :=
status_msg_out :=
END edit_entry;
END blog;
/
The underlying table structure the procedures are using is:
CREATE SEQUENCE blog_id_seq
INCREMENT BY
/
CREATE TABLE blogs (
id NUMBER PRIMARY KEY
title VARCHAR
date_published DATE
text CLOB
);
/
CREATE SEQUENCE category_id_seq
INCREMENT BY
CREATE TABLE categories (
id NUMBER PRIMARY KEY
name VARCHAR
);
/
CREATE TABLE blogs_to_categories (
blog_id INTEGER NOT NULL
REFERENCES blogs(id)
category_id INTEGER NOT NULL
REFERENCES categories(id)
PRIMARY KEY (blog_id
);
/
存儲過程和引用游標 看一下 blog
<?php
$conn = oci_connect(
$sql =
$stmt = oci_parse($conn
// Bind the input num_entries argument to the $max_entries PHP variable
$max_entries =
oci_bind_by_name($stmt
// Create a new cursor resource
$blog_entries = oci_new_cursor($conn);
// Bind the cursor resource to the Oracle argument
oci_bind_by_name($stmt
// Execute the statement
oci_execute($stmt);
// Execute the cursor
oci_execute($blog_entries);
print
// Use OCIFetchinto in the same way as you would with SELECT
while ($entry = oci_fetch_assoc($blog_entries
print_r($entry);
}
?>
存儲過程和 LOB Oracle Long 對象與存儲過程之間可以進行相互傳遞
<?php
$conn = oci_connect(
$sql =
$stmt = oci_parse($conn
$title =
oci_bind_by_name($stmt
oci_bind_by_name($stmt
oci_bind_by_name($stmt
oci_bind_by_name($stmt
// Explained in the next example
$Categories = oci_new_collection($conn
oci_bind_by_name($stmt
// Create a new lob descriptor object
$textLob = oci_new_descriptor($conn
// Bind it to the parameter
oci_bind_by_name($stmt
// Execute the statement but do not commit
oci_execute($stmt
// The status parameter will be negative if the procedure encountered a problem
if ( !$status ) {
// Rollback the procedure
oci_rollback($conn);
die (
}
// Save the body of the blog entry to the CLOB
if ( !$textLob
// Rollback the procedure
oci_rollback($conn);
die (
}
// Everything OK so commit
oci_commit($conn);
print $status_msg
?>
正如該腳本所演示的
CREATE OR REPLACE TYPE list_of_numbers AS VARRAY(
該定義允許您一次最多向
append
assign
assignElem
free
getElem
max
size
trim
此處
<?php
$conn = oci_connect(
$sql =
$stmt = oci_parse($conn
$id =
$title =
oci_bind_by_name($stmt
oci_bind_by_name($stmt
oci_bind_by_name($stmt
oci_bind_by_name($stmt
$textLob = oci_new_descriptor($conn
oci_bind_by_name($stmt
// Create an OCI
$Categories = oci_new_collection($conn
// Append some category IDs to the collection;
$Categories
$Categories
$Categories
// Bind the collection to the parameter
oci_bind_by_name($stmt
oci_execute($stmt
if ( !$status ) {
oci_rollback($conn);
die (
}
if ( !$textLob
oci_rollback($conn);
die (
}
oci_commit($conn);
print $status_msg
?>
結論 您現在已經了解了有關如何從 PHP 中調用存儲過程(既包括只涉及標量數據類型的簡單過程
From:http://tw.wingwit.com/Article/program/Oracle/201311/16569.html