0

如何处理 sql server 2008 查询中的动态输入值。

我的情况是在下面的查询中,我有两个输入参数从 java 通过 i-batis 传递

 1) if i enter stlmtTransId field alone query should execute (paymentTransId  is empty now).
 2) if i enter paymentTransId field alone query should execute(stlmtTransId  is empty now).
 3) if i enter both input values query should execute for matching the two inputs.

在任何上述情况下,我都需要输出。如何处理它。

  SELECT
    STLMT_TRANS.SETTLEMENT_TRANSACTION_ID
    ,STLMT_TRANS.PC_TRANSACTION_ID
    ,STLMT_TRANS.TRANSACTION_AMOUNT
    ,STLMT_TRANS.PAYMENT_AGENT_ID
    ,STLMT_TRANS.PAYMENT_AGENCY_ID
    ,STLMT_TRANS.PAYMENT_TRANS_DATE
    FROM
    T_SETTLEMENT_TRANSACTION STLMT_TRANS WITH (NOLOCK)
    WHERE
    STLMT_TRANS.SETTLEMENT_TRANSACTION_ID=#stlmtTransId#
    AND
    STLMT_TRANS.PC_TRANSACTION_ID=#paymentTransId#

注意:我使用 java + I-Batis

4

2 回答 2

0
WHERE
(
 (STLMT_TRANS.SETTLEMENT_TRANSACTION_ID=#stlmtTransId#) or (#stlmtTransId# is NULL)
)
AND
(
  (STLMT_TRANS.PC_TRANSACTION_ID=#paymentTransId#) or (#paymentTransId# is NULL)
)
于 2013-01-09T13:48:16.473 回答
0
SELECT
STLMT_TRANS.SETTLEMENT_TRANSACTION_ID
,STLMT_TRANS.PC_TRANSACTION_ID
,STLMT_TRANS.TRANSACTION_AMOUNT
,STLMT_TRANS.PAYMENT_AGENT_ID
,STLMT_TRANS.PAYMENT_AGENCY_ID
,STLMT_TRANS.PAYMENT_TRANS_DATE
FROM
T_SETTLEMENT_TRANSACTION STLMT_TRANS WITH (NOLOCK)
WHERE STLMT_TRANS.SETTLEMENT_TRANSACTION_ID = ISNULL(@stlmtTransId, STLMT_TRANS.SETTLEMENT_TRANSACTION_ID)
AND STLMT_TRANS.PC_TRANSACTION_ID = ISNULL(@paymentTransId, STLMT_TRANS.PC_TRANSACTION_ID)
于 2013-01-09T14:02:41.413 回答