您的位置:首页 > 数据库 > Oracle

oracle程序开发基础一

2006-06-22 11:54 369 查看
基础
什么是PL/SQL?
PL/SQL的英文全称是(Procedural Language Structured Query Language),是过程语言结构化查询语言,是直接驻留在数据库
中的编程环境。

下面是PL/SQL编程中应注意的方面:

1、变量附值是使用":="符号。

通用格式:  variable_name        variable_type  :=value;
例如:                i          number         :=0;

常用的数据类型:varchar2,number,date,boolean.

2、控制结构
if逻辑结构
一、
if  条件   then  
   语句;
end if;

例如:
if  l_date>'12-APR-03'  then
  语句;
end if

注意:与很多其他编程语言不同,if后面不直接加括号
但是可以这样用:
if  not(l_date<='12-APR-03') then
    语句;
end if

与上面语句等效

二、
if    条件    then
   语句;
 else
   语句; 
end if;

三、
if  条件   then
   语句;
elsif 条件    then
   语句;
end if; 

循环结构
一、
while循环

while   条件   loop
  语句;
end loop;
二、
for循环(非常特殊)
for l_counter in 1..5  --循环5次,变量l_counter的值从1一直递增到5.(在in 后面加 reverse,可以递减计数)
loop
    语句;
end loop;

一个例子:

使用serveroutput和dbms_output函数
首先把输出工具打开:set serveroutput on size 100000
在sql下输入如下:
SQL> declare
  2  l number;
  3  begin
  4  for l in reverse 1..5
  5  loop
  6  dbms_output.put_line(l);
  7  end loop;
  8  end;
     /

就可以看到运行结果了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息