使用 groovy soap ui 的静态 mysql 查询中的动态变量参数

Dynamic variable parameter in static mysql query using groovy soap ui

我想生成针对 BeneficiaryID 'ABC123' 的查询以及一些其他输入(如果它们也已给出)。假设如果给出了货币值,我想在 JOIN 查询中也包括货币条件,以及类别。我在 SOAP UI Groovy 脚本 .

中有以下代码片段
    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'

并且当给出货币和类别时(比如 USD & Commercial),然后是下面的查询。

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