zoukankan      html  css  js  c++  java
  • Oracle存储过程返回结果集

     Oracle存储过程返回结果集,需要把游标作为存储过程参数

     1.创建pakage 

    CREATE OR REPLACE PACKAGE pkg_query AS TYPE cur_query IS REF CURSOR;
    END pkg_query;
    

     2.创建存储过程()

    create or replace procedure Sp_Students
           (
            p_name       in  varchar2,     --姓名(传入参数,根据姓名筛选数据)
            p_totalRecords     out Number,     --总记录数(返回结果集的总条数)
            v_cur out pkg_query.cur_query   --返回的结果集
            )   
    
    is
       v_sql VARCHAR2(2000) := '';      --sql语句
    
    BEGIN
      
      v_sql:='SELECT TO_NUMBER(COUNT(1)) FROM tb_students t where t.name= '''|| p_name ||''' ' ;
     
      EXECUTE IMMEDIATE v_sql INTO p_totalRecords;
    
       v_sql := 'select * from tb_students t where t.name='''|| p_name ||''' ' ;
    
      -- DBMS_OUTPUT.put_line(v_sql);
       OPEN v_cur FOR v_sql;
    
    END  Sp_Students;

    3.c#调用存储过程

            /// <summary>
            /// 返回结果集
            /// </summary>
            /// <param name="name">存储过程传入参数,用于筛选数据</param>
            /// <param name="count">存储过程返回参数,返回筛选出来的数据总条数</param>
            /// <returns></returns>
            public DataSet RunProcedure(string name,out int count)
            {
                count = 0;
    string connectionString = "Data Source=orcl;User ID=用户名;Password=口令;Unicode=True";
    using (OracleConnection connection = new OracleConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); OracleDataAdapter sqlDA = new OracleDataAdapter("Sp_Students", connection); OracleParameter[] paras ={ new OracleParameter("p_name",name), new OracleParameter("p_totalRecords",count), new OracleParameter("v_cur", OracleType.Cursor) //返回的游标 }; paras[1].Direction = ParameterDirection.Output; paras[2].Direction = ParameterDirection.Output; sqlDA.SelectCommand.Parameters.AddRange(paras); sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;//设置 使用存储过程 sqlDA.Fill(dataSet); connection.Close(); count = Convert.ToInt32(paras[1].Value); return dataSet; } }

    4.测试存储过程

       4.1 创建表:

    -- Create table
    create table TB_STUDENTS
    (
      ID   NVARCHAR2(32) default sys_guid() not null,
      NAME NVARCHAR2(32),
      AGE  NUMBER
    );
    commit;
    --插入测试数据
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('DE3A7D4D5D054992B40761BFA2391F5', '张三', 20);
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('137ACF3D6C0A4BE0B71423A1F7D8452', '李四', 22);
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('2AF70A53BCD348EE9E8FF03916D018F', '王五', 18);
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('2877652AE0A3408A981D8980AE833CF', '100', 55);
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('7B356A73B80C4ED0BFAFA1DD96256E0', '100', 12);
    insert into TB_STUDENTS (ID, NAME, AGE)
    values ('1FF0A99B47FF4056BBDA7E3B4262C61', '100', 26);
    commit;

        4.2 c#调用:

                DataTable dt = null;
                int count ;
                DataSet ds = RunProcedure("100",out count);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
  • 相关阅读:
    不懂就问」CPU 到底是怎么识别代码的?
    StackOverflow经典问题:代码中如何去掉烦人的“!=null"判空语句
    在 Excel 中使用正则表达式进行查找与替换
    从浅入深详解独立ip网站域名恶意解析的解决方案
    CentOS配置防火墙操作实例
    三步解决fiddler升级后https无法通过证书验证问题
    apache http跳转到https代码
    openssl命令行将pfx格式转.key和.crt文件,Apache适用
    宝塔面板常用命令大全!
    CentOS如何挂载硬盘
  • 原文地址:https://www.cnblogs.com/bweb/p/4747037.html
Copyright © 2011-2022 走看看