• <i id='4GPvC'><tr id='4GPvC'><dt id='4GPvC'><q id='4GPvC'><span id='4GPvC'><b id='4GPvC'><form id='4GPvC'><ins id='4GPvC'></ins><ul id='4GPvC'></ul><sub id='4GPvC'></sub></form><legend id='4GPvC'></legend><bdo id='4GPvC'><pre id='4GPvC'><center id='4GPvC'></center></pre></bdo></b><th id='4GPvC'></th></span></q></dt></tr></i><div id='4GPvC'><tfoot id='4GPvC'></tfoot><dl id='4GPvC'><fieldset id='4GPvC'></fieldset></dl></div>

    <small id='4GPvC'></small><noframes id='4GPvC'>

      1. <legend id='4GPvC'><style id='4GPvC'><dir id='4GPvC'><q id='4GPvC'></q></dir></style></legend>

          <bdo id='4GPvC'></bdo><ul id='4GPvC'></ul>
        <tfoot id='4GPvC'></tfoot>
      2. 在 SYS_REFCURSOR 中执行动态 sql 语句

        时间:2023-11-02
      3. <legend id='CsVBr'><style id='CsVBr'><dir id='CsVBr'><q id='CsVBr'></q></dir></style></legend>

            <bdo id='CsVBr'></bdo><ul id='CsVBr'></ul>
                1. <tfoot id='CsVBr'></tfoot>

                2. <i id='CsVBr'><tr id='CsVBr'><dt id='CsVBr'><q id='CsVBr'><span id='CsVBr'><b id='CsVBr'><form id='CsVBr'><ins id='CsVBr'></ins><ul id='CsVBr'></ul><sub id='CsVBr'></sub></form><legend id='CsVBr'></legend><bdo id='CsVBr'><pre id='CsVBr'><center id='CsVBr'></center></pre></bdo></b><th id='CsVBr'></th></span></q></dt></tr></i><div id='CsVBr'><tfoot id='CsVBr'></tfoot><dl id='CsVBr'><fieldset id='CsVBr'></fieldset></dl></div>

                    <tbody id='CsVBr'></tbody>

                  <small id='CsVBr'></small><noframes id='CsVBr'>

                  本文介绍了在 SYS_REFCURSOR 中执行动态 sql 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以在 plsql 中执行动态 sql 并将结果返回到 sys_refcursor 中?到目前为止,我已经粘贴了我的尝试,但无法正常工作,这是我通过 Java 应用程序时出现的错误

                  is it possible to execute a dynamic piece of sql within plsql and return the results into a sys_refcursor? I have pasted my attempt soo far, but dosnt seam to be working, this is the error im getting throught my java app

                  ORA-01006:绑定变量没有存在 ORA-06512: 在LIVEFIS.ERC_REPORT_PK",第 116 行ORA-06512:在第 1 行

                  ORA-01006: bind variable does not exist ORA-06512: at "LIVEFIS.ERC_REPORT_PK", line 116 ORA-06512: at line 1

                  但这可能会被java误解,一切都可以很好地编译,所以我不确定.

                  but that could be somthing misconstrued by java, everything seams to compile fine soo im not sure.

                   procedure all_carers_param_dy (pPostcode in carer.postcode%type, pAge Number
                                                  ,pReport out SYS_REFCURSOR) is
                    begin
                      declare
                        lsql  varchar2(500) :='SELECT c.id FROM carer c, cared_for cf,carer_cared_for ccf '
                            ||' where c.id = ccf.carer_id (+)'
                            ||' AND cf.id (+) = ccf.cared_for_id';
                  
                      begin
                  
                       if pPostcode is not null and pAge <= 0 then
                          lsql := lsql||' AND c.postcode like ''%''|| upper(pPostcode)||''%''';
                        elsif pPostcode is null and pAge > 0 then 
                           lsql := lsql||' AND ROUND((MONTHS_BETWEEN(sysdate,c.date_of_birth)/12)) = pAge';
                        elsif pPostcode is not null and pAge > 0 then
                           lsql := lsql ||' AND ROUND((MONTHS_BETWEEN(sysdate,c.date_of_birth)/12)) = pAge'
                                        ||' AND c.postcode like ''%''|| upper(pPostcode)||''%''';
                        end if;
                  
                  
                          execute immediate lsql
                          into pReport;
                  
                  
                      end;
                    end;
                  

                  我是 plsql 的新手,甚至是动态 sql 的新手,所以任何帮助/建议都将不胜感激.

                  Im new to plsql and even newer to dynamic sql soo any help/ suggestions would be greatly apreciated.

                  再次感谢

                  乔恩

                  推荐答案

                  你必须绑定参数 pAgepPostcode.在动态 SQL 中,您可以用冒号 (:) 作为前缀.如果您使用 EXECUTE IMMEDIATEOPEN ... FOR,您将通过位置绑定参数,这就是我在示例中将它们重命名为 :P1 和 :P2 的原因:

                  you will have to bind the parameters pAge and pPostcode. In dynamic SQL you would prefix them with a colon (:). If you use EXECUTE IMMEDIATE or OPEN ... FOR, you will bind your parameters via position, this is why I renamed them :P1 and :P2 in the example:

                  DECLARE
                     lsql VARCHAR2(500) := 'SELECT c.id 
                                              FROM carer c, cared_for cf, carer_cared_for ccf 
                                             WHERE c.id = ccf.carer_id (+)
                                               AND cf.id (+) = ccf.cared_for_id';
                  BEGIN
                     IF pPostcode IS NULL THEN
                        lsql := lsql || ' AND :P1 IS NULL';
                     ELSE
                        lsql := lsql || ' AND c.postcode like ''%''|| upper(:P1)||''%''';
                     IF pPostcode pAge > 0 THEN
                        lsql := lsql || ' AND :P2 = ROUND((MONTHS_BETWEEN(sysdate,
                                                                          c.date_of_birth)/12))';
                     ELSE
                        lsql := lsql || ' AND nvl(:P2, -1) <= 0';
                     END IF;
                     OPEN pReport FOR lsql USING pPostcode, pAge;
                  END;
                  

                  注意:绑定变量的数量和位置必须在编译时知道,这就是为什么我经常使用上面的构造(即使不使用也将参数添加到其位置).向查询添加重言式(如 AND :P1 IS NULL)不会影响其解释计划.

                  Note: The number and position of bind variables has to be known at compile time, this is why I often use the construct above (adding the parameter to its position even if it is not used). Adding a tautology (as in AND :P1 IS NULL) to a query won't affect its explain plan.

                  这篇关于在 SYS_REFCURSOR 中执行动态 sql 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 pl/sql 过程中声明变量时出现语法错误 下一篇:Oracle PL/SQL TABLE 类型的 TO_CHAR

                  相关文章

                    <legend id='mWWw3'><style id='mWWw3'><dir id='mWWw3'><q id='mWWw3'></q></dir></style></legend>

                    <small id='mWWw3'></small><noframes id='mWWw3'>

                  1. <tfoot id='mWWw3'></tfoot>

                      <bdo id='mWWw3'></bdo><ul id='mWWw3'></ul>
                  2. <i id='mWWw3'><tr id='mWWw3'><dt id='mWWw3'><q id='mWWw3'><span id='mWWw3'><b id='mWWw3'><form id='mWWw3'><ins id='mWWw3'></ins><ul id='mWWw3'></ul><sub id='mWWw3'></sub></form><legend id='mWWw3'></legend><bdo id='mWWw3'><pre id='mWWw3'><center id='mWWw3'></center></pre></bdo></b><th id='mWWw3'></th></span></q></dt></tr></i><div id='mWWw3'><tfoot id='mWWw3'></tfoot><dl id='mWWw3'><fieldset id='mWWw3'></fieldset></dl></div>