[英]SQL Query with LIKE - Mule ESB
I am trying to execute a below SQL (MS-SQL) Query with Mule JDBC component.我正在尝试使用 Mule JDBC 组件执行以下 SQL (MS-SQL) 查询。 As the query has a LIKE operator, I included a '%' at the end to match any records in DB.由于查询有一个 LIKE 运算符,我在末尾包含了一个 '%' 以匹配数据库中的任何记录。 But when I run it throws the following error.但是当我运行它时会引发以下错误。 Please help me how to get the query executed with LIKE and %.请帮助我如何使用 LIKE 和 % 执行查询。
NOTE:
(1) Mule doesn't throw error, when the query is configured without %.
(2) SQL Server Studio executes the query successfully, with % like below.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Property file~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GET_RULES_SQL_QRY=SELECT * FROM RULES WHERE FROM_LKP = #[FROM] AND SUBJECT_LKP LIKE #[SUBJECT]%
~~~~~~~~~~~~~~~~~~~~~~~~MULE.xml~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Below is the mule-config.xml:
<flow name="testflow" doc:name="testflow">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8091" doc:name="HTTP"/>
<set-session-variable variableName="FROM" value="#['test@testemail.com']" doc:name="Session Variable"/>
<set-session-variable variableName="SUBJECT" value="#['Test column value']" doc:name="Session Variable"/>
<logger message="${GET_RULES_SQL_QRY}" level="INFO" category="GET_RULES_SQL_QRY" doc:name="Logger"/>
<jdbc-ee:outbound-endpoint
exchange-pattern="request-response" queryKey="getRule"
queryTimeout="-1" connector-ref="Rules_SQL_Database" doc:name="Load_Rules">
</jdbc-ee:outbound-endpoint>
<logger level="INFO" doc:name="Logger"/>
</flow>
~~~~~~~~~~~~~~~~~~~~~~~~~Error~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
INFO 2015-05-28 15:26:05,421 [[TEST-api].connector.http.mule.default.receiver.02] GET_RULES_SQL_QRY: SELECT * FROM IMAP_RULES WHERE FROM_LKP = test@testemail.com AND SUBJECT_LKP LIKE Test column value%
ERROR 2015-05-28 15:26:05,449 [[TEST-api].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
Exception stack is:
1. Incorrect syntax near '%'.(SQL Code: 102, SQL State: + S0001) (com.microsoft.sqlserver.jdbc.SQLServerException)
com.microsoft.sqlserver.jdbc.SQLServerException:196 (null)
2. Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://getRule, connector=EEJdbcConnector
{
name=IMAP_Rules_SQL_Database
lifecycle=start
this=8cd6d78
numberOfConcurrentTransactedReceivers=4
createMultipleTransactedReceivers=false
connected=true
supportedProtocols=[jdbc]
serviceOverrides=<none>
}
, name='endpoint.jdbc.getRule', mep=REQUEST_RESPONSE, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: String (org.mule.api.transport.DispatchException)
org.mule.transport.AbstractMessageDispatcher:109 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '%'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SQL Query in SSMS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM RULES WHERE FROM_LKP = 'test@testemail.com' AND SUBJECT_LKP LIKE 'Test column value %'
---------------------------------------------------------------------
COLUMN1 | COLUMN2 | COLUMN3 |
---------------------------------------------------------------------
test@testemail.com |Test column value still contd | sample table |
---------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mule 3.8 - Parameterized query Mule 3.8 - 参数化查询
I know this is very old but you could do something like this to use the LIKE clause in your query:我知道这很旧,但是您可以执行以下操作以在查询中使用 LIKE 子句:
select * from test
where col1 like #["%"+flowVars.searchString+"%"]
Essentially, you concatenate "%" on the sides where you want to have the wild card search.本质上,您在要进行通配符搜索的两侧连接“%”。 The above code worked with MySQL 5.7 - I am hoping you can take the inspiration and make the syntax for MSSQL which I think shouldn't be different in this case.上面的代码适用于 MySQL 5.7 - 我希望你能从中汲取灵感并为 MSSQL 制定语法,我认为在这种情况下不应该有所不同。 Sorry, I am not good with MSSQL.抱歉,我不擅长 MSSQL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.