zoukankan      html  css  js  c++  java
  • 数据库基础系列之五:PL/SQL入门(2)

    //比较零碎,先记下了,以后再系统地去学习

    1.在存储过程中使用游标返回结果集:

    (1)包:

    create or replace package PKG_ReturnSet

    as

    type MyCurType is ref cursor;

    end;

     

    (2)存储过程:

    create or replace procedure SP_ReturnSet(p_Result out PKG_ReturnSet.MyCurType)

    as

    begin

    open p_Result for 'select * from alarminfo';

    end;

     

    (3)程序:

    static void Main(string[] args)

    {

    OracleConnection conn = new OracleConnection("……");

    try

    {               

    OracleCommand cmd = new OracleCommand();

    cmd.Connection = conn;

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.CommandText = "SP_ReturnSet";

    OracleParameter p = new OracleParameter("p_Result", OracleType.Cursor);

    p.Direction = ParameterDirection.Output;

    cmd.Parameters.Add(p);

    OracleDataAdapter da = new OracleDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);

     

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

    {

    Console.WriteLine(ds.Tables[0].Rows[i]["obj_id"].ToString());

    }

    }

    catch (Exception x)

    {

    Console.WriteLine(x.Message);

    }

    finally

    {

    if (conn.State == ConnectionState.Open)

    {

    //conn.Close();

    }

    }

    Console.Read();

    }

     

     

    2.游标参数:

    declare

    cursor Cur_1(p_StationID varchar2) is

    select * from MyTable where ID= p_StationID;

     

    begin

    for v_Row in Cur_1('dmz1') loop

           DBMS_OUTPUT.put_line(v_Row.name);

    end loop;

    end;

    ((1)在游标for循环中,游标的打开、获取、关闭都是隐含地进行,不需要你书写显式的代码来执行。(2)变量v_Row无须显式地声明。)

     

     

    3.另一些PL/SQL简单概念:

    (1)PL/SQL代码组织而成“”,如果你创建存储过程或包,那么它是一个“命名块”,否则,称为“匿名块”。

    PL/SQL块包含三个部分:(1)声明:定义及初始化变量及游标,(2)可执行命令:使用流程控制关键字来执行命令并将值赋于已声明的变量,(3)异常处理。如下:

    declare

    <declarations section>

    begin

    <executable commands>

    exception

    <exception handling>

    end;

     

     

    (2)流程控制(If)

    If……then

    ……;

    Elsif……then

    ……;

    Else

    ……;

    End If;

     

     

    (3)流程控制(循环)

    简单循环:循环直到遇到ExitExit When

    For循环:循环执行指定次数

    While循环:在符合条件下循环

    --------------------------------------

    Loop

           ……;

    Exit When ……;

    End Loop;

    --------------------------------------

    For radius in 1..7 Loop

    area:=pi*power(radius,2);

    insert into areas values(radius,area)

    End Loop;

    --------------------------------------

    While radius<=7 Loop

    ……;

    End Loop;

     

     

    (4)流程控制(Case)

    Case When …… Then

    ……;

    When …… Then

           ……;

    Else ……;

    End Case;

     

     

  • 相关阅读:
    【计算机网络】物理层
    【计算机网络】计算机网络体系结构
    【计算机组成原理】存储器
    【计算机组成原理】定点数运算
    【计算机组成原理】校验码
    【计算机组成原理】数据的表示和运算
    【计算机组成原理】计算机系统概述
    【计算机组成原理】输入输出系统(一)
    【计算机组成原理】总线
    【计算机组成原理】中央处理器-硬布线控制器 流水线
  • 原文地址:https://www.cnblogs.com/morvenhuang/p/619497.html
Copyright © 2011-2022 走看看