How to add a dynamic parameter in Mule4 Select Query - mulesoft

I have a SELECT query for which the entire WHERE condition is coming from a Java class. How can I use the entire WHERE condition in the SELECT query? This is what I tried:
SELECT * FROM EMPLOYEE_TABLE WHERE 1=1 #[payload]
Here payload is a WHERE clause coming from a Java class: NAME = VKP AND STATE = PA AND CITY = KOP
I am getting an error message like " parameter null was not bound for query select…", but I am able to see the payload value coming out from the Java class and in the logs.

You should not use an expression directly in the query. Instead assign the query in an expression to a variable, then use the variable as the query.
For example something like:
<set-variable value="#['SELECT * FROM EMPLOYEE_TABLE WHERE 1=1 ' ++ payload]" name="query" />
<db:select ...>
<db:sql>#[vars.query]</db:sql>
</db:select>
Note that you risk having SQL injections vulnerabilities doing this.

Related

Error: an expression of non-boolean type specified in a context where a condition is expected, near ',' ; Using Cascading Parameters

I am trying to use cascading parameters to filter this Pivot table in SSRS Report Builder.
This is the main dataset query:
SELECT * FROM(
SELECT *,
CASE WHEN PLN=PLN THEN '01. GROSS PREMIUM'
ELSE NULL
END AS PREMIUM
FROM MYI
PIVOT (SUM(GROSS_PREMIUM_1) FOR PERIOD_TYPE IN([MTD],[YTD],[ITD])) pvt
UNION ALL
SELECT *,
CASE WHEN PLN=PLN THEN '02. REFUND'
ELSE NULL
END AS PREMIUM
FROM MYI
PIVOT (SUM(REFUND_2) FOR PERIOD_TYPE IN([MTD],[YTD],[ITD])) pvt
)A
WHERE ACC_PERIOD = #ACC_PERIOD
AND REINSURER IN (#REINSURER)
AND INSURER IN (#INSURER)
AND PLN IN (#PLN)
AND SVC_AGY IN (#SVC_AGY)
These parameters in the WHERE clause each have their own dataset and are cascaded from top to bottom.
Here is the query for the final parameter as an example:
SELECT DISTINCT [fasren_servicingagency]
FROM [FAS_ReinsuranceNumber]
WHERE ACC_PERIOD = #ACC_PERIOD
AND REINSURER IN (#REINSURER)
AND INSURER IN (#INSURER)
AND PLN IN (#PLN)
ORDER BY SVC_AGY
The query runs fine in SSMS, but I keep getting this error when I run it in the SSRS report builder:
An expression of non-boolean type specified in a context where a condition is expected, near ','
It must have something to do with the WHERE clause in either the main query or the parameter dataset queries. I have seen many posts about this error but I could not find a solution that helped with my problem. Any ideas?
NOTE: all parameters are character fields, no integers, no date columns.
EDIT: There is another query that I was successful with;
SELECT [acctyymm],
[retrosummary],
[retroagent],
[coveragedivision],
[policyinsurer],
SUM([NetDue]) AS NET_DUE,
SUM([ActualPayable]) AS PAYABLE
FROM [dbo].[RetroNumberGAAP]
WHERE [acctyymm] IN (#ACC_PERIOD)
AND [retrosummary] IN (#RETSUM)
AND [retroagent] IN (#RETAGT)
AND [coveragedivision] IN (#PLN)
AND [policyinsurer] IN (#INSURER)
AND POWER([fasrtng_NetDue],2)+POWER([ActualPayable],2)<>0
GROUP BY [acctyymm],
[retrosummary],
[retroagent],
[coveragedivision],
[policyinsurer]
How am I successful running the report with cascaded parameters in this second query and not the first query?
I figured out what I did wrong. My Dataset properties were not configured correctly in the Report Builder.
WRONG CONFIGURATION
Dataset properties; Parameters; parameter name: SVC_AGY Parameter Value: [#SVC_AGY]
CORRECTED CONFIGURATION
Dataset properties; Parameters; parameter name: #SVC_AGY Parameter Value: [#SVC_AGY]
Make sure your parameter names match your parameter values, including the # symbol.

Pass string parameter to SQL over jdbc query in pentaho CDE

I'm trying to pass a string parameter obtained by another query to another query in the same dashboard but when I use the parameter no results was selected. What is the proper syntax or where is the problem.
I'm beginner in pentaho biserver-CE. I use version 6.1. with JDBC connection to SQL Server 2016 SP1.
Now I'm making a sales dashboard from invoices and i want to make a dynamic filter by the time units (which works fine) and by the country shortcuts (which didn't work). If i pass shortcut of some country directly with quotes it works fine but when I replace it by the parameter it doesn't select nothing (Also in CDA preview). When I made some reports in Jasper i used exclamation inside of the parameter to pass quoted value but here I didn't find anything like this for Pentaho.
select top 10
invrow.item as ITEM,
invrow.agent as AGENT,
sum(invrow.qt) as MEASURE,
sum(invrow.val) as VALUE
from invrow
left join invhead on invrow.type = invhead.type
and invrow.nr = invhead.nr
left join art on invrow.item = art.item
where left(invhead.date,4) = ${year}
and invhead.country like ${Country}
group by invrow.item, invrow.agent, invhead.country
order by MEASURE DESC
parameter ${Country} was acquired by another query by the same field. There is a query to gain the parameter:
select distinct
invhead.country
from
invhead
where
left(invhead.date,4) = ${year}
The original query shows nothing but when I Replace parameter ${Country} by for example 'UK' like this.
It works fine:
select top 10
invrow.item as ITEM,
invrow.agent as AGENT,
sum(invrow.qt) as MEASURE,
sum(invrow.val) as VALUE
from invrow
left join invhead on invrow.type = invhead.type
and invrow.nr = invhead.nr
left join art on invrow.item = art.item
where left(invhead.date,4) = ${year}
and invhead.country like 'UK'
group by invrow.item, invrow.agent, invhead.country
order by MEASURE DESC
Now when I use parameter I have nothing in the select list but there are rows, which should be selected.
The syntax is correct, but that way of passing parameters only works for single valued parameters.
To pass an array of parameters, as you seem to be trying, you need to pass the parameter as right hand side of a in operator,
(...)
and invhead.country in ( ${Country} )
(...)
and set the parameter to be of type StringArray.

SQL XML parsing using a attribute value supplied by another field in the same row

Context: I'm scraping some XML form descriptions from a Web Services table in hopes of using that name to identify what the user has inputted as response. Since this description changes for each step (row) of the process and each product I want something that can evaluate dynamically.
What I tried: The following was quite useful but it returns a dynamic attribute query result in it's own field ans using a coalesce to reduce the results as one field would lead to it's own complications: Get values from XML tags with dynamically specified data fields
Current Attempt:
I'm using the following code to generate the attribute name that I will use in the next step to query the attribute's value:
case when left([Return], 5) = '<?xml'
then lower(cast([Return] as xml).value('(/response/form/*/#name)[1]','varchar(30)'))
else ''
end as [FormRequest]
And as part of step 2 I have used the STUFF function to try and make the row-level query possible
case when len(FormRequest)>0
then stuff( ',' + 'cast([tmpFormResponse] as xml).value(''(/wrapper/#' + [FormRequest] + ')[1]'',''varchar(max)'')', 1, 1, '')
else ''
end as [FormResponse]
Instead of seeing 1 returned as my FormReponse feild value for the submit attribute (please see in yellow below) it's returning the query text -- cast([tmpFormResponse] as xml).value('(/wrapper/#submit)1','varchar(max)') -- instead (that which should be queried).
How should I action the value method so that I can dynamically strip out the response per row of XML data in tmpFormResponse based on the field value in the FormRequest field?
Thanx
You can check this out:
DECLARE #xml XML=
N'<root>
<SomeAttributes a="a" b="b" c="c"/>
<SomeAttributes a="aa" b="bb" c="cc"/>
</root>';
DECLARE #localName NVARCHAR(100)='b';
SELECT sa.value(N'(./#*[local-name()=sql:variable("#localName")])[1]','nvarchar(max)')
FROM #xml.nodes(N'/root/SomeAttributes') AS A(sa)
Ended up hacking up a solution to the problem by using PATINDEX and CHARINDEX to look for the value in the [FormRequest] field in the he tmpFormResponse field.

passing parameters to VO SQL transient attribute

I created a view object for scott.emp table
I added a transient attribute, then change the default value to be from SQL and wrote statement like this:
select max(nvl(sal, 0)) from emp where deptno = view.deptno
my problem is: how to pass view.deptno as parameter to sql statement ?
running the page ,I have got an error.
ORA-00904: "EMP_VO"."DEPTNO": invalid identifier
I solved this by inspiration from previous answer, as I wrote in sql field in Default values the following clause by using Entity attribute so it works.
select max(nvl(sal, 0)) from emp where deptno = Emp_EO.DEPTNO
In my first post I used View object attribute in where clause, but this throws above error ORA-00904.
the condition values in where clause must be entity attribute not view attribute.
Emp_EO.DEPTNO
If you check your VO's query, it should looks similar to this:
SELECT Emp.EMPNO,
Emp.ENAME,
Emp.JOB,
Emp.MGR,
Emp.HIREDATE,
Emp.SAL,
Emp.COMM,
Emp.DEPTNO,
(select max(nvl(sal, 0)) from emp where deptno = Emp.DEPTNO)
FROM EMP Emp

Update SQL Server table using XML data

From my ASP.Net application I am generating XML and pass it as input data to stored procedure as below,
<Aprroval>
<Approve>
<is_nb_approved>false</is_nb_approved>
<is_approved>true</is_approved>
<is_submitted>true</is_submitted>
<UserId>35</UserId>
<ClientId>405</ClientId>
<taskDate>2015-05-23T00:00:00</taskDate>
</Approve>
<Approve>
<is_nb_approved>false</is_nb_approved>
<is_approved>true</is_approved>
<is_submitted>true</is_submitted>
<UserId>35</UserId>
<ClientId>405</ClientId>
<taskDate>2015-05-24T00:00:00</taskDate>
</Approve>
</Approval>
And below is my stored procedure,
create procedure UpdateTaskStatus(#XMLdata XML)
AS
UPDATE [TT_TaskDetail]
SET
is_approved=Row.t.value('(is_approved/text())[1]','bit'),
is_nb_approved=Row.t.value('(is_nb_approved/text())[1]','bit'),
is_submitted=Row.t.value('(is_submitted/text())[1]','bit')
FROM #XMLdata.nodes('/Aprroval/Aprrove') as Row(t)
WHERE user_id = Row.t.value('(UserId/text())[1]','int')
AND client_id = Row.t.value('(ClientId/text())[1]','int')
AND taskdate = Row.t.value('(taskDate/text())[1]','date')
But when I execute this stored procedure, I am getting return value as 0 and no record is getting updated. Any suggestions welcome.
You have 2 errors in your xml:
First is nonmatching root tags.
Second, more important, you are quering nodes('/Aprroval/Aprrove'), but inner tag is Approve not Aprrove.
Fiddle http://sqlfiddle.com/#!3/66b08/3
Your outer tags do not match. Your opening tag says, "Aprroval" instead of "Approval". Once I corrected that, I was able to select from the XML without issue.

Resources