3

@ConfListTable 是一个表值参数 (TVP),它有一个确认码列表。我想从 PmtHist 表中选择所有记录,其中确认码也在@ConfListTable 中。以下代码适用于此;没问题。

Select * from PmtHist
Where Confirmation in(
    Select Str1 as ConfirmationCode
    From @ConfListTable
    )

我的问题是:PmtHist 中的确认代码有时会在实际确认代码之后“无效”。像“ab321voided”但我也很想要那些记录。如何修改上述查询以获取与@ConfListTable 中的记录匹配或匹配@ConfListTable + 'voided' 的记录?

4

4 回答 4

3

一种快速简便的方法是简单地使用 REPLACE:

Select * 
from PmtHist
Where REPLACE(Confirmation, 'voided', '') in(
    Select Str1 as ConfirmationCode
    From @ConfListTable
)
于 2013-08-23T10:33:26.283 回答
1
select *
from PmtHist ph
where exists (
    select 1 from @ConfListTable
    where ph.Confirmation in (Str1, Str1 + 'voided')
)
于 2013-08-23T10:34:29.497 回答
1

为了获得更好的性能,我建议使用两个单独的查询

SELECT *
FROM dbo.PmtHist t1
WHERE t1.Confirmation IN (SELECT t2.Str1 FROM @ConfListTable t2)
UNION ALL
SELECT *
FROM dbo.PmtHist t1
WHERE t1.Confirmation IN (SELECT t2.Str1 + 'voided' FROM @ConfListTable t2)

见演示SQLFiddle

于 2013-08-23T12:24:18.570 回答
0

添加一个“不在的地方”:

Select * from PmtHist
Where Confirmation in(
    Select Str1 as ConfirmationCode
    From @ConfListTable
    )
and Confirmation not in(
    <some list of voided confirmation
    )
于 2013-08-23T10:42:13.017 回答