2017-02-16 25 views
1

我想爲BeneficiaryID'ABC123'生成查詢以及其他一些輸入(如果還給出了)。假設如果給出貨幣值,我想在JOIN查詢中包含貨幣條件,以及類別。我在SOAP UI Groovy腳本中有以下代碼片段。在使用groovy soap的靜態mysql查詢中的動態變量參數ui

query= " CORR.BeneficiaryID LIKE 'ABC123'" 
    if (currencyValue!=""){ 
    query=query + " and CORR.Currency LIKE '${currencyValue}'" 
    } 
    if (CategoryValue!=""){ 
    query=query + " and CORR.Category LIKE '${CategoryValue}'" 
    } 
    log.info("Query" + query) 

    Outputrows = sql.rows("select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\ 
    from BENEFICIARY CORR \ 
    JOIN LOCATION LOC on CORR.UID=LOC.UID and ${query} 

    log.info("Output rows size" + Outputrows.size()) 

當貨幣和類別沒有給出,我想有以下查詢運行並得到結果。

select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\ 
from BENEFICIARY CORR \ 
JOIN LOCATION LOC on CORR.UID=LOC.UID and CORR.BeneficiaryID LIKE 'ABC123' 

,貨幣和類別給予時(說美元&商業),然後下面的查詢。

select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\ 
from BENEFICIARY CORR \ 
JOIN LOCATION LOC on CORR.UID=LOC.UID and CORR.BeneficiaryID LIKE 'ABC123' and CORR.Currency LIKE 'USD' and CORR.Category LIKE 'Commercial' 

我在Outputrows.size()的結果上看到的所有結果都爲零。

你能否指正我,我在哪裏做錯了。

謝謝。

回答

0

這是更改腳本。

由於問題只是建立查詢,只有把該部分刪除sql執行部分,因爲這不是真正的問題。

//Define the values or remove if you get those value from somewhere else 
//Just put them here to demonstrate 
//You may also try by empty value to make sure you are getting the right query 
def currencyValue = 'USD' 
def categoryValue = 'Commercial' 
def query = 'select CORR.Preferred as preferred, CORR.Category as category,CORR.Currency as currency from BENEFICIARY CORR JOIN LOCATION LOC on CORR.UID = LOC.UID and CORR.BeneficiaryID LIKE \'ABC123\'' 
currencyValue ? (query += " and CORR.Currency LIKE '${currencyValue}'") : query 
categoryValue ? (query += " and CORR.Category LIKE '${categoryValue}'") : query 
log.info "Final query is \n ${query}" 

您只需通過query進一步,你需要運行SQL,說sql.rows(query)

你可以快速嘗試Demo

+0

感謝饒..這是工作,我打算是。 – Y5288