设为首页 收藏本站
查看: 432|回复: 0

[经验分享] Oracle/PLSQL: Procedure that outputs a dynamic PLSQL cursor

[复制链接]
YunVN网友  发表于 2016-8-15 06:44:27 |阅读模式
Oracle/PLSQL: Procedure that outputs a dynamic PLSQL cursor

  Question: In Oracle, I have a table called "wine" and a stored procedure that outputs a cursor based on the "wine" table.
  I've created an HTML Form where the user can enter any combination of three values to retrieve results from the "wine" table. My problem is that I need a general "select" statement that will work no matter what value(s), the user enters.
Example:

  parameter_1= "Chianti" parameter_2= "10" parameter_3= wasn't entered by the user but I have to use in the select statement. And this is my problem. How to initialize this parameter to get all rows for column3?

  The output of my stored procedure must be a cursor.
  Answer: To solve your problem, you will need to output a dynamic PLSQL cursor in Oracle.
  Let's take a look at how we can do this. We've divided this process into 3 steps.

Step 1 - Table Definition
  First, we need a table created in Oracle called "wine". Below is the create statement for the wine table.

  create table wine ( col1 varchar2(40),  col2 varchar2(40),  col3 varchar2(40) );

  We've made this table definition very simple, for demonstration purposes.

Step 2 - Create package
  Next, we've created a package called "winepkg" that contains our cursor definition. This needs to be done so that we can use a cursor as an output parameter in our stored procedure.


create or replace PACKAGE winepkg IS    /* Define the REF CURSOR type. */    TYPE wine_type IS REF CURSOR RETURN wine%ROWTYPE; END winepkg;
/* the common method is like this */
create or replace PACKAGE wine_pkg
AS
TYPE IBS_CURSOR IS REF CURSOR;
END wine_pkg;

This cursor will accept all fields from the "wine" table.

Step 3 - Create stored procedure
  Our final step is to create a stored procedure to return the cursor. It accepts three parameters (entered by the user on the HTML Form) and returns a cursor (c1) of type "wine_type" which was declared in Step 2.
  The procedure will determine the appropriate cursor to return, based on the value(s) that have been entered by the user (input parameters).


create or replace procedure find_wine2   (
col1_in in varchar2,   
col2_in in varchar2,   
col3_in in varchar2,   
c1 out winepkg.wine_type
)
as

BEGIN

/* all columns were entered */    IF (length(col1_in) > 0) and (length(col2_in) > 0) and (length(col3_in) > 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col1 = col1_in       and  wine.col2 = col2_in       and  wine.col3 = col3_in;

/* col1 and col2 were entered */    ELSIF (length(col1_in) > 0) and (length(col2_in) > 0) and (length(col3_in) = 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col1 = col1_in       and  wine.col2 = col2_in;

/* col1 and col3 were entered */    ELSIF (length(col1_in) > 0) and (length(col2_in) = 0) and (length(col3_in) > 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col1 = col1_in       and  wine.col3 = col3_in;

/* col2 and col3 where entered */    ELSIF (length(col1_in) = 0) and (length(col2_in) > 0) and (length(col3_in) > 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col2 = col2_in       and  wine.col3 = col3_in;

/* col1 was entered */    ELSIF (length(col1_in) > 0) and (length(col2_in) = 0) and (length(col3_in) = 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col1 = col1_in;

/* col2 was entered */    ELSIF (length(col1_in) = 0) and (length(col2_in) > 0) and (length(col3_in) = 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col2 = col2_in;

/* col3 was entered */    ELSIF (length(col1_in) = 0) and (length(col2_in) = 0) and (length(col3_in) > 0)    THEN       OPEN c1 FOR       select *       from wine       where wine.col3 = col3_in;

END IF; END find_wine2;


protected void Button1_Click(object sender, EventArgs e)
{
string connString = "Data Source=joe;User ID=scott;Password=tiger;";
DataSet ds = new DataSet();
OracleConnection conn = new OracleConnection(connString);
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
string c1 = "Oracle";
string c2 = "SQL";
string c3 = "scott";
cmd.CommandText = "find_wine2";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("col1_in", OracleType.NVarChar)).Direction = ParameterDirection.Input;
cmd.Parameters.Add(new OracleParameter("col2_in", OracleType.NVarChar)).Direction = ParameterDirection.Input;
cmd.Parameters.Add(new OracleParameter("col3_in", OracleType.NVarChar)).Direction = ParameterDirection.Input;
cmd.Parameters.Add(new OracleParameter("c1", OracleType.Cursor)).Direction = ParameterDirection.Output;
cmd.Parameters[0].Value = c1;
cmd.Parameters[1].Value = c2;
cmd.Parameters[2].Value = c3;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.TableMappings.Add("Table", "wine");
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

<!-- InstanceEndEditable -->

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-257776-1-1.html 上篇帖子: 【转帖】 Oracle 查询表空间使用情况 下篇帖子: Oracle 表空间的增删改查等操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表