Frage

Say, I have a jMeter variable var equals 123.
I also have a query stored in a sql file:

SELECT * FROM Table WHERE ID = ${var}

All I need is to read that query from the file and evaluate ${var} into actual value, and then execute it in JDBC Sampler. So I need to combine these two pieces into

SELECT * FROM Table WHERE ID = 123

and pass the query to JDBC sampler.

Though, jMeter doesn't evaluate the ${var} parameter stored in that sql file and all I can pass to JDBC sampler is (obvious one):

SELECT * FROM Table WHERE ID = ${var}.

Does anyone know how to make jMeter evaluate the stored variable into actual value?

War es hilfreich?

Lösung

I had a similar requirement.

You need to use Beanshell Preprocessor for the JDBC sampler. Copy the below script and put it in a .bsh file and call it. I assumed you have the query stored in 'SQLQuery' variable.

I tested the below script and it works.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

String regex = "\\$\\{([^}]+)\\}";
SQLQuery = vars.get("SQLQuery");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(SQLQuery);
while(matcher.find())
{
    String token = matcher.group();   //${var}
    String tokenKey = matcher.group(1);  // var
    SQLQuery = SQLQuery.replaceFirst(Pattern.quote(token), Matcher.quoteReplacement(vars.get(tokenKey)));
}
vars.put("SQLQuery", SQLQuery);

Andere Tipps

You need to use a "Prepared Select Statement" as Query Type. Prepared Statements

Then replace your ${var} with ?

SELECT * FROM Table WHERE ID = ?

And add the 2 parameters at the bottom.

Parameter values: ${var}
Parameter types:  INT
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top