| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1610 人关注过本帖
标题:[分享]PL/SQL的基本特点(初学者请进)
只看楼主 加入收藏
marydan
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-2-14
收藏
 问题点数:0 回复次数:2 
[分享]PL/SQL的基本特点(初学者请进)
1. PL/SQL的块结构

P L / S Q L程序的基本结构是块。所有的P L / S Q L程序都是由块组成的,这些块之间还可以相互
嵌套。通常,程序中的每一块都实现一个逻辑操作,从而把不同的任务进行分割,由不同的块
来实现。P L / S Q L的块结构如下所示:
DECLARE
/* Declarative section - PL/SQL variables, types, cursors,
and local subprograms go here. */
BEGIN
/* Executable section - procedural and SQL statements go here.
This is the main section of the block and the only one
that is required. */
EXCEPTION
/* Exception-handling section - error-handling statements go
here. */
END;
在上面演示的块结构中,只有执行部分是必须的,声明部分和异常处理部分都是可选的。
块结构中的执行部分至少要有一个可执行语句。P L / S Q L块采用的这种分段结构将P L / S Q L程序的
不同功能各自独立出来。
P L / S Q L的这种特点是仿效第三代程序设计语言A d a采用的程序结构。在A d a语言中使用的很
多程序结构包括A d a使用的块结构都适用于P L / S Q L语言。除此之外,在P L / S Q L语言中还可以发
现A d a语言使用的异常处理方法、过程和函数声明以及包的定义的语法等特征。


2. 错误处理
P L / S Q L块中的异常处理部分是用来响应应用程序运行中遇到的错误。把程序的主体部分与
错误处理部分代码相互隔离,这样,程序的结构看起来十分清晰。例如,下面的P L / S Q L块演示
了将异常发生的时间及将遇到该异常错误的用户名记录在日志表的处理过程。
节选自在线代码Error.sql
DECLARE
v_ErrorCode NUMBER; -- Code for the error
v_ErrorMsg VARCHAR2(200); -- Message text for the error
v_CurrentUser VARCHAR2(8); -- Current database user
v_Information VARCHAR2(100); -- Information about the error
BEGIN
/* Code that processes some data here */
EXCEPTION
WHEN OTHERS THEN
-- Assign values to the log variables, using built-in
-- functions.
v_ErrorCode := SQLCODE;
v_ErrorMsg := SQLERRM;
v_CurrentUser := USER;
v_Information := 'Error encountered on ' ||
TO_CHAR(SYSDATE) || ' by database user ' || v_CurrentUser;
-- Insert the log message into log_table.
INSERT INTO log_table (code, message, info)
VALUES (v_ErrorCode, v_ErrorMsg, v_Information);
END;
注意上面的例子和许多其他例程都在本书的联机发布信息中。读者如要了解有关详细
信息,请参阅1 . 3 . 3的内容。

3. 变量和类型
信息在数据库与P L / S Q L程序之间是通过变量进行传递的。所谓变量就是可以由程序读取或
赋值的存储单元。在上面的例子中, v _ C u r r e n t U s e r, v _ E r r o r C o d e ,和v _ I n f o r m a t i o n都是变量。通
常,变量是在P L / S Q L块的声明部分定义的。
每个变量都有一个特定的类型与其关联。变量的类型定义了变量可以存放的信息类别。如
下所示,P L / S Q L变量可以与数据库列具有同样的类型:
DECLARE
v_StudentName VARCHAR2(20);
v_CurrentDate DATE;
v_NumberCredits NUMBER(3);
P L / S Q L变量也可以是其他类型:
DECLARE
v_LoopCounter BINARY_INTEGER;
v_CurrentlyRegistered BOOLEAN;
除此之外, P L / S Q L还支持用户自定义的数据类型,如记录类型、表类型等。使用用户自定
义的数据类型可以让你定制程序中使用的数据类型结构。下面是一个用户自定义的数据类型例
子:
DECLARE
TYPE t_StudentRecord IS RECORD (
FirstName VARCHAR2(10),
LastName VARCHAR2(10),
CurrentCredits NUMBER(3)
);
v_Student t_StudentRecord;

4. 循环结构
P L / S Q L支持多种循环结构。所谓循环就是指可以重复执行的同一代码段。例如,下面的程
序块使用一个简单的循环来把数字1~5 0插入到表t e m p _ t a b l e中:
节选自在线代码SimpleLoop.sql
DECLARE
v_LoopCounter BINARY_INTEGER := 1;
BEGIN
LOOP
INSERT INTO temp_table (num_col)
VALUES (v_LoopCounter);
v_LoopCounter := v_LoopCounter + 1;
EXIT WHEN v_LoopCounter > 50;
END LOOP;
END;
P L / S Q L中还提供了一种使用F O R的循环结构,这种循环的结构更简单。如下所示,我们可
以使用这种F O R循环来实现上面的循环操作:
节选自在线代码NumricLoop.sql
BEGIN
FOR v_LoopCounter IN 1..50 LOOP
INSERT INTO temp_table (num_col)
VALUES (v_LoopCounter);
END LOOP;
END;

5. 游标
游标是用来处理使用S E L E C T语句从数据库中检索到的多行记录的工具。借助于游标的功能,
数据库应用程序可以对一组记录逐个进行处理,每次处理一行。例如,下面的程序块可以检索
到数据库中所有学生的名和姓:
节选自在线代码CursorLoop.sql
DECLARE
v_FirstName VARCHAR2(20);
v_LastName VARCHAR2(20);
-- Cursor declaration. This defines the SQL statement to
-- return the rows.
CURSOR c_Students IS
SELECT first_name, last_name
FROM students;
BEGIN
-- Begin cursor processing.
OPEN c_Students;
LOOP
-- Retrieve one row.
FETCH c_Students INTO v_FirstName, v_LastName;
-- Exit the loop after all rows have been retrieved.
EXIT WHEN c_Students%NOTFOUND;
/* Process data here */
END LOOP;
-- End processing.
CLOSE c_Students;
END;


搜索更多相关主题的帖子: SQL section 特点 结构 
2006-02-24 11:50
marydan
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-2-14
收藏
得分:0 
下面这个游标的使用是我自己总结的,其中可选项都没有写,大家可以在联机文档里查,sql server的那个实例也是联机文档的,oracle的那个,我自己写的,照葫芦画瓢的:)



sql server中定义cursor:

DECLARE cursor_name CURSOR
FOR select_statement



oracle中定义cursor:

DECLARE
CURSOR cursor_name IS
select_statement;




sql server中使用cursor:

OPEN cursor_name
FETCH NEXT FROM cursor_name INTO var_name
WHILE @@FETCH_STATUS=0
print var_name
CLOSE cursor_name
DEALLOCATE cursor_name


实例:

下例显示如何嵌套游标以生成复杂的报表。为每个作者声明内部游标。

SET NOCOUNT ON

DECLARE
@au_id varchar(11),
@au_fname varchar(20),
@au_lname varchar(40),
@message varchar(80),
@title varchar(80)

PRINT "-------- Utah Authors report --------"

DECLARE authors_cursor CURSOR FOR
SELECT au_id, au_fname, au_lname
FROM authors
WHERE state = "UT"
ORDER BY au_id

OPEN authors_cursor

FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT " "
SELECT @message = "----- Books by Author: " +
@au_fname + " " + @au_lname

PRINT @message

-- Declare an inner cursor based
-- on au_id from the outer cursor.

DECLARE titles_cursor CURSOR FOR
SELECT t.title
FROM titleauthor ta, titles t
WHERE ta.title_id = t.title_id AND
ta.au_id = @au_id -- Variable value from the outer cursor

OPEN titles_cursor
FETCH NEXT FROM titles_cursor INTO @title

IF @@FETCH_STATUS <> 0
PRINT " <<No Books>>"

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @message = " " + @title
PRINT @message
FETCH NEXT FROM titles_cursor INTO @title
END

CLOSE titles_cursor
DEALLOCATE titles_cursor
-- Get the next author.
FETCH NEXT FROM authors_cursor
INTO @au_id, @au_fname, @au_lname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

-------- Utah Authors report --------
----- Books by Author: Anne Ringer
The Gourmet Microwave
Is Anger the Enemy?
----- Books by Author: Albert Ringer
Is Anger the Enemy?
Life Without Fear




oracle中使用游标的格式:
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO var_name
EXIT WHEN cursor_name%NOTFOUND;

END LOOP;
CLOSE cursor_name;
END




oracle的游标实例:
下例显示如何嵌套游标以生成复杂的报表。为每个作者声明内部游标。

declare
v_au_id varchar2(11);
v_au_fname varchar2(20);
v_au_lname varchar2(40);
v_title varchar2(80);
cursor authors_cursor is
select au_id, au_fname, au_lname
from authors
where state = "UT"
order by au_id;
begin
open authors_cursor;

loop
fetch authors_cursor into v_au_id,v_au_fname,v_au_lname;
exit when authors_cursor%notfound ;
dbms_output.put_line( '----- Books by Author: ' ||v_au_fname || ' ' || v_au_lname);

begin
declare cursor titles_cursor is
select t.title
from titleauthor ta, titles t
where ta.title_id = t.title_id and
ta.au_id = v_au_id;

begin
open titles_cursor;

loop

fetch titles_cursor into v_title;
exit when titles_cursor%notfound
dbms_output.put_line(v_title);
end loop
end
end
end loop
 end

[此贴子已经被作者于2006-2-24 13:05:47编辑过]


我的眼里只有你
2006-02-24 11:58
孙大圣
Rank: 2
等 级:新手上路
威 望:4
帖 子:127
专家分:0
注 册:2006-2-23
收藏
得分:0 
不错

非学无以广才,非志无以成学
2006-03-30 16:30
快速回复:[分享]PL/SQL的基本特点(初学者请进)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023196 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved