存储过程中拼接的变量和点的问题

存储过程中的变量:

以   ''|| 变量名 ||'' 的形式。

点的解释:

一个点' 表示是字符串。

两个点''连接变量,之后再加一个点',最后编译成‘变量值’。

存储过程代码:

    --说明:把店铺操作界面上选择的店铺插入到线上订单数据库的店铺表里
    --作者:杨斌
    --日期:2012-08-09
    procedure insertSelectedShops(
             strSelectedShopID in varchar2,      --店铺操作界面上选择的店铺ID,是"店铺ID1,店铺ID2,店铺ID3......"的形式。
             out_error_row out number,           --错误行
             out_error_msg out varchar2          --错误信息
             )          
    as
   
      --ref_cur_sql varchar2(4000);  --执行选择店铺sql语句
      nowtime date;--操作日期 修改记录
                  
      sql_str varchar2(4000);      
    begin
   
      out_error_row :=0;--错误行
      out_error_msg :='';--错误信息
     
      select sysdate into nowtime from dual ;
     
      sql_str := ' insert into  T_XS_SHOP(' || chr(10) ||
                 'SHOP_ID,' || chr(10) ||    -- 店铺ID  
                 'PT_ID,'    ||  chr(10)||    -- * 平台ID
                 'TITLE,'   || chr(10) ||      -- 店铺名称
                 'APP_KEY,'  || chr(10) ||      -- * APP_KEY
                 'AppSecret,'|| chr(10) ||      -- * AppSecret
                 'SessionKey,'|| chr(10) ||      -- * SessionKey
                 'CREATETIME'|| chr(10) ||      -- * 店铺导入时间
                 ')' || chr(10) ||
                 '(' || chr(10) ||
                 'select shop_id,SHOP_TYPE_ID,TITLE,APPKEY,APPSECRET,SESSIONKEY,'''|| to_char(nowtime,'YYYY/MM/DD/hh24:mm:ss') ||''' from t_base_shopup@yb' || ' where shop_id in (' || strSelectedShopID || ')' ||
                 ')' ;
                
      out_error_row :=1;--错误行         
      dbms_output.put_line(sql_str);
      execute immediate sql_str;
      out_error_row :=2;--错误行
     
    exception
      when others then
        out_error_msg := '数据库错误:' || sqlerrm;
     
    end insertSelectedShops; 

举个正确的例子:

错误的例子:

会报 : 数据库错误ORA-00923 未找到要求的 FROM 关键字

另外一个存储过程的拼接例子:

这个例子的体现了逗号怎么拼接上。

      str_sql := ' merge into t_xs_trade t1 ' || chr(10) ||
      ' using (select tid from t_xs_trade where tid =  '''|| prm_tid ||''' ) t2 ' || chr(10) ||
      ' on (t1.tid = t2.tid) ' || chr(10) ||
          ' when matched then  '|| chr(10) ||
          '   update set  '|| chr(10) ||
          '     t1.shop_name                     =     '''||  prm_shop_name  ||''','|| chr(10) ||
          '     t1.urgent_task_name        =  '''|| prm_urgent_task_name ||'''' || chr(10) ||
          ' when not matched then '|| chr(10) ||
          '   insert( '|| chr(10) ||
          '     tid, '|| chr(10) ||
          '     urgent_task_name '|| chr(10) ||
          '   ) '|| chr(10) ||
          ' VALUES( '|| chr(10) ||
          '     '''|| prm_tid ||''','|| chr(10) ||
          '     ''' || prm_urgent_task_name ||''''|| chr(10) ||
          ' ) '  ;    
      
      out_error_row :=2;--错误行
      --返回ID
      prm_thradenumber := prm_tid;
      execute immediate str_sql; 
原文地址:https://www.cnblogs.com/YangBinChina/p/2631733.html