zoukankan      html  css  js  c++  java
  • Oracle_merge into 中 using 后的查询表如果有参数的情况

    Oracle_merge into 中 using 后的查询表如果有参数的情况。

    先说一下merge into 的基本语法

    /*语法:
    MERGE [INTO [schema .] table [t_alias]
    USING [schema .] { table | view | subquery } [t_alias]
    ON ( condition )
    WHEN MATCHED THEN merge_update_clause
    WHEN NOT MATCHED THEN merge_insert_clause;
    */

    一般的 using 后是跟着一个具体的表名,但实际的开发中有时是带参数的情况,

    以上的语法就不适合了,个人认为用动态的拼接字符串的方法,来解决问题。

     我们来举例解释:

    假设我们要 把店铺操作界面上选择的店铺插入到线上订单数据库的店铺表里,

    从界面上的操作返回的是"店铺ID1,店铺ID2,店铺ID3......"的形式。

    代码如下:

    View Code
     1     --说明:把店铺操作界面上选择的店铺插入到线上订单数据库的店铺表里
     2     --作者:杨斌
     3     --日期:2012-08-09
     4     procedure insertSelectedShops(
     5              strSelectedShopID in varchar2,      --店铺操作界面上选择的店铺ID,是"店铺ID1,店铺ID2,店铺ID3......"的形式。
     6              out_error_row out number,           --错误行
     7              out_error_msg out varchar2          --错误信息
     8              )           
     9     is 
    10     
    11       str_sql varchar2(4000);--定义查询SQL语句变量      
    12     begin
    13     
    14       out_error_row :=1;--错误行
    15       
    16       str_sql := ' merge into T_XS_SHOP t1 ' ||
    17       ' using (select shop_id,SHOP_TYPE_ID,TITLE,APPKEY,APPSECRET,SESSIONKEY from t_base_shopup@yb where shop_id in ('||strSelectedShopID||')) t2 ' ||
    18       ' on (t1.shop_id = t2.shop_id) ' ||
    19       ' when matched then ' ||
    20       '   update set t1.title =  t2.TITLE, ' ||
    21       '             t1.pt_id = t2.SHOP_TYPE_ID, ' ||
    22       '             t1.app_key = t2.APPKEY, ' ||
    23       '             t1.appsecret = t2.APPSECRET, ' ||
    24       '             t1.sessionkey = t2.SESSIONKEY, ' ||
    25       '             t1.createtime =  '''|| to_char(sysdate,'YYYY/MM/DD/hh24:mm:ss') ||
    26       ''' when not matched then ' ||
    27       '   insert (           ' ||
    28       '           SHOP_ID,   ' ||
    29       '           PT_ID,     ' ||
    30       '           TITLE,     ' ||
    31       '           APP_KEY,   ' ||
    32       '           AppSecret, ' ||
    33       '           SessionKey, ' ||  
    34       '           CREATETIME ' ||
    35       '           )          ' ||
    36       '           values     ' ||
    37       '           (          ' ||
    38       '           t2.shop_id,t2.SHOP_TYPE_ID,t2.TITLE,t2.APPKEY,t2.APPSECRET,t2.SESSIONKEY, ''' ||to_char(sysdate,'YYYY/MM/DD/hh24:mm:ss') ||
    39       '''           )  ';
    40                    
    41       out_error_row := 2;--错误行 
    42       
    43       execute immediate str_sql; 
    44       
    45       out_error_row := 3;--错误行 
    46       
    47     exception
    48       when others then 
    49         out_error_msg := '数据库错误:' || sqlerrm; 
    50       
    51     end insertSelectedShops;   

     代码解释:上面的代码using 中有一个参数strSelectedShopID,

    最后用execute immediate执行以下。

    一定要注意最后拼接的部分,如果是   '           );  ';   这样,就会报“数据库错误:ORA-00911: 无效字符”的错误。

  • 相关阅读:
    ----Vue 单页应用的首屏优化----
    ----小程序之rich-text图片宽度自适应----
    ----vue-router 如何在新窗口打开页面----
    ----element-ui实现时间控件开始时间和结束时间关联----
    ----element-ui自定义表单验证----
    ----js中的every和some----
    「Linux学习」之挂载访问samba服务
    「Linux学习」之samba和nfs共享服务搭建
    「Linux学习」之防火墙服务设置
    「linux学习」之批量创建用户脚本
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/2633724.html
Copyright © 2011-2022 走看看