在字符串中包含参数

时间:2016-02-29 04:56:19

标签: string postgresql parameters dynamic-sql postgresql-9.5

当我尝试在下面的字符串中包含参数时遇到问题:

sel:='SELECT partner_code,acc_date,journal_no,term_code,due_date,
      currency,curr_rate,amount,acc_amount,aging_months,acc_outstanding,category,
      doc_outstanding,partner_name,sales_person,ref_no1,ref_no2,description
    FROM 
    (SELECT 
       j1.partner_code,j1.acc_date,j1.journal_no,j1.term_code,COALESCE(j1.due_date,j1.acc_date) as due_date,
       j1.ref_no1,(case when c1.opt_master_acc=TRUE then j1.ref_no2 else '''' END) as ref_no2,j1.description,
       j1.currency,j1.curr_rate,j1.amount,j1.acc_amount,c1.category,
       p1.name as partner_name,
       (SELECT (SELECT ( SELECT (DATE_PART(''year'', ''' || aging_date::date ||'''::date)- DATE_PART(''year'',(CASE WHEN ' || use_duedate || '=true THEN COALESCE(j1.due_date,j1.acc_date) ELSE j1.acc_date END)::date) )* 12 ) + ( SELECT (DATE_PART(''month'', ''' || aging_date::date || '''::date)- DATE_PART(''month'',(CASE WHEN ' || use_duedate || '=true THEN COALESCE(j1.due_date,j1.acc_date) ELSE j1.acc_date END)::date) )))) as aging_months,
       (c1.opt_partner_drcr*(j1.acc_amount-j1.match_acc_amount))::decimal(19,4) AS acc_outstanding,
       a1.sales_rep as sales_person,p1.area, 
       ((CASE WHEN j1.curr_rate>0 THEN ROUND(c1.opt_partner_drcr*((acc_amount-match_acc_amount))/j1.curr_rate,2) ELSE 0 END)::decimal(19,4)) as doc_outstanding
      FROM acc_journal j1
      INNER JOIN acc_journal_cfg c1 ON j1.book_type=c1.book_type and j1.doc_type=c1.doc_type
      INNER JOIN PARTNER p1 ON j1.partner_code=p1.partner_code
      LEFT OUTER JOIN partner_acc a1 ON p1.partner_id=a1.partner_id 
      WHERE j1.book_type=''' || tran_code || ''' and j1.acc_amount <>0 AND
        j1.doc_type<>''ORM'' AND
        (' || acc_category || '=' || ' '''' OR  p1.partner_acccategory=acc_category)
        AND (j1.acc_date>=acc_start_date) 
        AND (j1.acc_date<=' || aging_date::date || ')  
        AND (j1.acc_amount<>j1.match_acc_amount)
        AND j1.rec_state>=0) AS SQL1';  

基本上错误在于:

ERROR:  syntax error at or near "="
LINE 22:         (= '' OR  p1.partner_acccategory=acc_category)
                  ^ 

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。它默认读取参数为'',这在字符串中没有任何内容,因此错误是这样的 - &gt; (=''或者a = b)

我发现的解决方案是包含一个CASE WHEN并添加更多单引号,以便在值为'时'''。代码如下:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>





 <label onclick="$('#asd').hide();" class="btn btn-info active ">
   <input type="radio" checked = 'checked' value="RM" id="posisi1" name="posisi"> RM
 </label>


<label onclick="$('#asd').show(); $('#parent').select2();" class="btn btn-info ">
     <input type="radio" value="Chief"  id="position2" name="posisi" > Chief
 </label>

<div class="form-group" style="display:none;" id = "asd">
                    <label class="col-sm-3 control-label">Pilih RM / Chief</label>
                    <div class="col-sm-8">
                    <select id="parent" class="form-control" name="parent">
                    <option value="none">None</option>

                      <option  value="120442130008">CHOIRUL ILYAS</option>

                      <option  value="1502017">CHOIRUL ILYAS</option>

                      <option  value="120442130009">Boby Kurniawan</option>
                    </select>
                     </div>
 </div>