我正在尝试使用Mule JDBC组件执行以下SQL(MS-SQL)查询。由于查询具有LIKE运算符,因此我在末尾添加了'%'来匹配数据库中的任何记录。但是当我运行它会引发以下错误。请帮助我如何使用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="#['[email protected]']" 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 = [email protected] 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 = '[email protected]' AND SUBJECT_LKP LIKE 'Test column value %'
---------------------------------------------------------------------
COLUMN1 | COLUMN2 | COLUMN3 |
---------------------------------------------------------------------
[email protected] |Test column value still contd | sample table |
---------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mule 3.8-参数化查询
我知道这已经很老了,但是您可以执行以下操作在查询中使用LIKE子句:
select * from test
where col1 like #["%"+flowVars.searchString+"%"]
基本上,您将“%”串联在您要进行通配符搜索的位置。上面的代码与MySQL 5.7一起使用-我希望您能从中得到启发并为MSSQL编写语法,我认为在这种情况下应该不会有所不同。抱歉,我对MSSQL不好。