【问题标题】:SQL Query with LIKE - Mule ESB使用 LIKE 进行 SQL 查询 - Mule ESB
【发布时间】:2020-03-24 11:54:31
【问题描述】:

我正在尝试使用 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="#['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  |
---------------------------------------------------------------------

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

【问题讨论】:

  • 您说传递给 SSMS 的查询是:LIKE 'Test column value %' 但是您的属性文件查询具有不带引号的 LIKE 字符串:LIKE #[SUBJECT]% 这有效吗?:LIKE '#[SUBJECT]%'
  • 是的,尽管 LOG 文件显示不带引号,但当 Mule 通过 java SQL 驱动程序调用查询时,它会将值放在引号内(据我的理解)。
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~属性文件~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [FROM] AND SUBJECT_LKP LIKE '#[SUBJECT]%'
  • 异常堆栈是:1.索引2超出范围。(SQL代码:0,SQL状态:+ S1093)(com.microsoft.sqlserver.jdbc.SQLServerException)com.microsoft.sqlserver .jdbc.SQLServerException:170 (null) 2. 无法通过端点路由事件:DefaultOutboundEndpoint{endpointUri=jdbc://getRule, connector=EEJdbcConnector
  • 我尝试使用引号,但 Mule 现在抛出了不同的异常。

标签: sql mule mule-el


【解决方案1】:

Mule 3.8 - 参数化查询

我知道这已经很老了,但是您可以这样做以在查询中使用 LIKE 子句:

select * from test
where col1 like #["%"+flowVars.searchString+"%"]

基本上,您可以在要进行通配符搜索的两侧连接“%”。上面的代码适用于 MySQL 5.7 - 我希望你能从中获得灵感并为 MSSQL 制定语法,我认为在这种情况下不应该有所不同。对不起,我不擅长 MSSQL。

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2014-09-12
    • 2012-08-11
    相关资源
    最近更新 更多