简体   繁体   中英

SQL Query with LIKE - Mule ESB

I am trying to execute a below SQL (MS-SQL) Query with Mule JDBC component. As the query has a LIKE operator, I included a '%' at the end to match any records in DB. But when I run it throws the following error. Please help me how to get the query executed with LIKE and %.

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

I know this is very old but you could do something like this to use the LIKE clause in your query:

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. Sorry, I am not good with MSSQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM