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

时间:2013-01-09 13:34:45

标签: sql-server-2008

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

我的场景是在下面的查询中我有两个输入的paramanetrs从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.

在abovr的任何情况下我都需要输出。如何处理它。

  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

2 个答案:

答案 0 :(得分:0)

WHERE
(
 (STLMT_TRANS.SETTLEMENT_TRANSACTION_ID=#stlmtTransId#) or (#stlmtTransId# is NULL)
)
AND
(
  (STLMT_TRANS.PC_TRANSACTION_ID=#paymentTransId#) or (#paymentTransId# is NULL)
)

答案 1 :(得分: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)