2013-01-09 140 views
0

如何處理sql server 2008查詢中的動態輸入值。如何處理sql server 2008查詢中的動態輸入值

我的情況是在下面的查詢是具有從Java傳遞兩個輸入paramanetrs IM至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# 

注:IM採用java + I-Batis

回答

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

@Valex。以上soltion是工作,但並不如預期。它的工作,如果我給兩個輸入欄。我的預期方案是1)單獨使用stlmtTransId字段或2)單獨使用paymentTransId字段或3)paymentTransId,stlmtTransId – Manu

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)