0

チルダで区切られた複数のオファーコードを含むことができる文字列が与えられました。親子関係を定義する場合と定義しない場合123~125~126
があるテーブルもあります。

tbl_allowed
============
offercode varchar(15)
parent_oc varchar(15)

ex:
offercode = 124
parent_oc = 126

区切り文字列を取得して分割する関数はすでにありますが、文字列を取得し、内容を内容と比較して、parent_oc列に存在する値をoffercode列のtbl_allowed値に置き換えて再生成したいと思います。 tbl_allowed。定義された関係が存在しない場合は、それ自体を使用してください。

これは、単一のオファーコードで非常に簡単です。

set @newOfferCode = (select top 1 coalesce(cac.offercode, @lOfferCode)
from tbl_allowed cac
where OfferCode = @lOfferCode or parent_oc = @lOfferCode)

select coalesce(nullif(@newOfferCode,''), @lOfferCode)

しかし、チルダで区切られた文字列があると問題が発生します。何か案は?

4

1 に答える 1

0

これが私がやったことです。動作しているようです。

    CREATE TABLE #tempoffers(OfferCode varchar(15), NewOfferCode varchar(15))

    INSERT INTO #tempoffers
    SELECT OutParam, null
    FROM dbo.SplitString(@lOfferCode, '~')

    update #tempoffers set NewOfferCode = coalesce(cac.offercode, mb.offerCode)
    from #tempoffers mb
    left outer join tbl_Allowed cac on mb.OfferCode = cac.parent_oc

            -- building the new string
    declare @newOfferCode varchar(5000)
    SELECT @newOfferCode = COALESCE(@newOfferCode + '~', '') + NewOfferCode FROM #tempoffers

    drop table #tempoffers
    SELECT @newOfferCode as OfferCode
于 2012-08-08T21:36:33.730 に答える